Jump to content

auto respawn player


Tails

Recommended Posts

So I want the player to respawn automatically after he dies, on the same position.

function deathCheck(source) 
    local x,y,z = getElementPosition(source) 
    if (isPedDead(source)) then 
    spawnPlayer(source, x,y,z) 
    end 
end 
addEventHandler("onPlayerWasted", deathCheck) 

What am I doing wrong?

Also, how do I attach a timer to the respawn?

Thanks in advance

Link to comment

Huh, so what do I need to do?

This is what I have:

function respawn(source) 
    local x,y,z = getElementPosition(source) 
    spawnPlayer(source, x, y, z) 
    setPedSkin (source, mySkin) 
end 
addCommandHandler("respawn", respawn) 
  
function deathCheck() 
    local x,y,z = getElementPosition(source) 
    if (isPedDead(source)) then 
    spawnPlayer(source, x,y,z) 
    end 
end 
addEventHandler("onPlayerWasted", deathCheck) 
     
  
function killcommand(source) 
    killPed (source, source) 
    outputChatBox("Type /respawn to respawn immediately.", getRootElement(), 255,0,0,true) 
end 
addCommandHandler("kill", killcommand) 
  

Link to comment

Try this:

function respawn ( thePlayer ) 
    if ( isPedDead ( thePlayer ) ) then 
        local x, y, z = getElementPosition ( thePlayer ) 
        spawnPlayer ( thePlayer, x, y, z ) 
        setElementModel ( thePlayer, mySkin ) 
    end 
end 
addCommandHandler ( "respawn", respawn ) 
  
function deathCheck ( ) 
    local x, y, z = getElementPosition ( source ) 
    spawnPlayer ( source, x, y, z ) 
end 
addEventHandler ( "onPlayerWasted", root, deathCheck ) 
  
function killcommand ( thePlayer ) 
    killPed ( thePlayer, thePlayer) 
    outputChatBox ( "Type /respawn to respawn immediately.", thePlayer, 255, 0, 0, true ) 
end 
addCommandHandler ( "kill", killcommand ) 

Link to comment

Nope , doesn't work.

Only thing that lets me respawn is by typing /respawn

I want the respawn to happen automatically after I die.

Edit:

This is the full script maybe that helps:

local spawnX, spawnY, spawnZ = 1713.02, 917.98, 10.80 
local mySkin = 50 
function joinHandler() 
    spawnPlayer(source, spawnX, spawnY, spawnZ) 
    fadeCamera(source, true) 
    setCameraTarget(source, source) 
    setPedSkin ( source, mySkin ) 
    outputChatBox("Welcome to My Server", source) 
end 
addEventHandler("onPlayerJoin", getRootElement(), joinHandler) 
  
function respawn(source) 
    local x,y,z = getElementPosition(source) 
    spawnPlayer(source, x, y, z) 
    setPedSkin (source, mySkin) 
end 
addCommandHandler("respawn", respawn) 
  
function deathCheck() 
    local x,y,z = getElementPosition(source) 
    if (isPedDead(source)) then 
    spawnPlayer(source, x,y,z) 
    end 
end 
addEventHandler("onPlayerWasted", deathCheck) 
     
  
function killcommand(source) 
    killPed (source, source) 
    outputChatBox("Type /respawn to respawn immediately.", getRootElement(), 255,0,0,true) 
end 
addCommandHandler("kill", killcommand) 
  
  
  
  
  
-- retrieve a table with all flag elements 
local flagElements = getElementsByType ( "tdm" ) 
-- loop through them 
for key, value in pairs(flagElements) do 
    -- get our info 
    local posX = getElementData ( value, "posX" ) 
    local posY = getElementData ( value, "posY" ) 
    local posZ = getElementData ( value, "posZ" ) 
end 

Link to comment

use ' setElementPosition ' much better than ' spawnPlayer '

try this

    local spawnX, spawnY, spawnZ = 1713.02, 917.98, 10.80 
    local mySkin = 50 
    function joinHandler() 
        setElementPosition(source, spawnX, spawnY, spawnZ) 
        fadeCamera(source, true) 
        setCameraTarget(source, source) 
        setPedSkin ( source, mySkin ) 
        outputChatBox("Welcome to My Server", source) 
    end 
    addEventHandler("onPlayerJoin", getRootElement(), joinHandler) 
      
    function respawn(source) 
        local x,y,z = getElementPosition(source) 
        setElementPosition(source, x, y, z) 
        setPedSkin (source, mySkin) 
    end 
    addCommandHandler("respawn", respawn) 
      
    function deathCheck() 
        local x,y,z = getElementPosition(source) 
        if (isPedDead(source)) then 
        setElementPosition(source, x,y,z) 
        end 
    end 
    addEventHandler ("onPlayerWasted", root, deathCheck) 
        
      
    function killcommand(source) 
        killPed (source, source) 
        outputChatBox("Type /respawn to respawn immediately.", getRootElement(), 255,0,0,true) 
    end 
    addCommandHandler("kill", killcommand) 
      
      
      
      
      
    -- retrieve a table with all flag elements 
    local flagElements = getElementsByType ( "tdm" ) 
    -- loop through them 
    for key, value in pairs(flagElements) do 
        -- get our info 
        local posX = getElementData ( value, "posX" ) 
        local posY = getElementData ( value, "posY" ) 
        local posZ = getElementData ( value, "posZ" ) 
    end 

Found the problem, you have missing 'root' at addEventHandler of 'onPlayerWasted'.
addEventHandler ( "onPlayerWasted", root, deathCheck ) 

fixed

Link to comment

Sorry for the many questions, (I am such a noob), but can you help me figure out how to put a respawn timer in the script below?

function deathCheck() 
        local x,y,z = getElementPosition(source) 
        if (isPedDead(source)) then 
        spawnPlayer(source, x,y,z) 
        setPedSkin (source, mySkin) 
        outputChatBox("Type /respawn to respawn immediately.", getRootElement(), 255,0,0,true) 
        setTimer ( spawnPlayer, 2500, 1, source) 
        end 
    end 
    addEventHandler ("onPlayerWasted", root, deathCheck) 

Link to comment
Sorry for the many questions, (I am such a noob), but can you help me figure out how to put a respawn timer in the script below?
  
  
 function deathCheck() 
        local x,y,z = getElementPosition(source) 
        if (isPedDead(source)) then 
        spawnPlayer(source, x,y,z) 
        setPedSkin (source, mySkin) 
        outputChatBox("Type /respawn to respawn immediately.", getRootElement(), 255,0,0,true) 
        setTimer ( spawnPlayer, 2500, 1, source) 
        end 
    end 
    addEventHandler ("onPlayerWasted", root, deathCheck) 
  
  
  

  
  
  
 function deathCheck() 
        local x,y,z = getElementPosition(source) 
          outputChatBox("Type /respawn to respawn immediately.", getRootElement(), 255,0,0,true) 
        setTimer ( function() 
        spawnPlayer(source, x,y,z) 
        setElementAlpha(source, 0) 
         end 
, 2500, 1) 
    end 
    addEventHandler ("onPlayerWasted", root, deathCheck) 
  
  

Link to comment
Sorry for the many questions, (I am such a noob), but can you help me figure out how to put a respawn timer in the script below?
function deathCheck() 
        local x,y,z = getElementPosition(source) 
        if (isPedDead(source)) then 
        spawnPlayer(source, x,y,z) 
        setPedSkin (source, mySkin) 
        outputChatBox("Type /respawn to respawn immediately.", getRootElement(), 255,0,0,true) 
        setTimer ( spawnPlayer, 2500, 1, source) 
        end 
    end 
    addEventHandler ("onPlayerWasted", root, deathCheck) 

Yours is half fine, you should remove the first spawnPlayer, setPedSkin and fill the arguments in the timer.

function deathCheck() 
        local x,y,z = getElementPosition(source) 
        if (isPedDead(source)) then 
        outputChatBox("Type /respawn to respawn immediately.", getRootElement(), 255,0,0,true) 
        setTimer ( spawnPlayer, 2500, 1, source, x, y, z, 0, mySkin ) 
        end 
    end 
    addEventHandler ("onPlayerWasted", root, deathCheck) 

Link to comment
  • Other Languages Moderators
Another question, what exactly is the 0 standing for in this line:
setTimer ( spawnPlayer, 2500, 1, source, x, y, z, 0, mySkin ) 

?

rotation: rotation of the player on spawn.

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...