Jump to content

[HELP] setTimer On Player Respawn


Whizz

Recommended Posts

Hello, I want the player to respawn after a certain time.
Thank you for helping me.

function respawn ()
local team = getPlayerTeam(source)
 if team == grove then
    local x = 268.868
    local y = 2037.208
    local z = 17.7
    local groveskins = math.random(1, 4)
    spawnPlayer(source, x, y, z)
    fadeCamera(source, true)
    setCameraTarget(source, source)
    setPlayerTeam(source, grove)
    giveWeapon(source, 31, 500)
    outputChatBox("", source, 255, 255, 255)
     if groveskins == 1 then
      setElementModel(source, 101)
     end
     if groveskins == 2 then
      setElementModel(source, 102)
     end
     if groveskins == 3 then
      setElementModel(source, 103)
     end
     if groveskins == 4 then
      setElementModel(source, 107)
     end
 end
 if team == police then
    local x = 270.871
    local y = 1937.740
    local z = 17.7
    local policeskins = math.random(1, 4)
    spawnPlayer(source, x, y, z)
    fadeCamera(source, true)
    setCameraTarget(source, source)
    setPlayerTeam(source, police)
    outputChatBox("", source, 255, 255, 255)
    giveWeapon(source, 31, 500)
     if policeskins == 1 then
      setElementModel(source, 100)
     end
     if policeskins == 2 then
      setElementModel(source, 104)
     end
     if policeskins == 3 then
      setElementModel(source, 105)
     end
     if policeskins == 4 then
      setElementModel(source, 106)
     end
 end
end
addEventHandler("onPlayerWasted", getRootElement(), respawn)

Link to comment

If you need to execute the same code you have, but with a delay, then you can wrap it in a function that's inside a setTimer call, like this:

function respawn()
	--[[ source variable won't exist after the event handler finishes
	so it won't be available inside the timer function, therefore we
	retain its value by creating a local variable "player" and
	assigning the value to it ]]
	local player = source

	setTimer(function()
		-- whatever code you need to execute
	end, 10000, 1)
end
addEventHandler("onPlayerWasted", getRootElement(), respawn)

And because the source variable won't exist when the timer executes, you need to replace all occurrences with player.

Edited by DiSaMe
  • Sad 1
Link to comment

But does the player respawn immediately, with your code, or does it not work either? I don't see grove and police variables being defined anywhere, so if this is the full script, it's understandable why it doesn't work. You either need to store the teams in these variables (for example, you can use getTeamFromName) so one of them would compare equal to team variable, or check the team in some other way, like instead of comparing the team element, you can retrieve its name and compare that, like this:

local team = getPlayerTeam(source)
local teamName = getTeamName(team)
if teamName == "Grove" then
	-- ...
end
if teamName == "Police" then
	-- ...
end

But then it depends on what exactly the names of the teams are, because they have to be exactly the same to compare equal.

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...