Jump to content

Player Respawn


Recommended Posts

Hello guys. I made a script, very simple onPlayerWasted respawn the player.

When i'm disconnecting while im dead, and trying to logIn, the server just froze ("NETWORK TROUBLE").

And i get these debug messages:

fsda.png

 

the screen is just black, and after a few seconds the player respawning.

Whats the problem? Here is the code:

 

addEventHandler("onPlayerJoin",root,function()
    spawn(source)
end)
--< Spawn >--
function spawn(source)
    x, y, z = 2028.4606933594, 1342.2176513672, 10.8203125
    repeat until spawnPlayer(source,x+1,y+1,z, 0,303)
    setCameraTarget(source, player)
    fadeCamera(source, true) 
    setElementRotation(source, 0, 0, -90)
end
addEventHandler("onPlayerWasted",root,function()
    exports["rc_core"]:takeMoney(source,5000)
    skin = getElementModel(source) or 0
    setTimer(respawn, 5000, 1, source)
    setTimer(setElementModel,5000,1,source,skin)
end)

--< Respawn >--
function respawn(source)
    x, y, z = 2270.298828125, 1399.2269287109, 42.8203125
    repeat until spawnPlayer(source,x+1, y+1,z, 0, math.random(1,1) )
    setCameraTarget(source, player)
    fadeCamera(source, true) 
    setElementRotation(source, 0, 0, -90)
end

 

Edited by TheMOG
Link to comment
4 hours ago, f8upd8 said:

setTimer(respawn, 5000, 1, source)

setTimer(setElementModel,5000,1,source,skin)

 

Syntax is incorrect. If function needs any arguments, use timer like this:
 


setTimer(function ()

setElementModel(source, skin)

end, 5000, 1)

 

And just for your sake, wipe out of the world's face this:

 


repeat until spawnPlayer(source,x+1, y+1,z, 0, math.random(1,1) )

 

Thank you.

Why should i wipe that?

 

Also,

bad argument @ "setElementModel" [expected element at argument 1 ,got nil]

 

Also2//

Deleted the repeat until, but  if i reconnect when im dead i'm getting a 100 hp.

Edited by TheMOG
Link to comment
2 hours ago, Gordon_G said:

So, if I understand correctly. When you are dead and you disconnect you want to be dead when you reconnect ?

Use accountData or SQL.

I'm saving player datas. The problem is, when player reconnect while dead. I want to make that, when player login, and health 0, spawning the player instant

Now i added this to the saving system

 

    if isPedDead(source) or (getElementHealth(source)) < 1 then 
        setElementHealth(source,100)
        setAccountData(account,"acc.health",100)    
    end    

 

Also, i added "if isElement(source) then ....

so, how can i make that possible, if player disconnecting while dead, the system  cancelEvent the setTimer

addEventHandler("onPlayerWasted",root,function()
    if isElement(source) then
        exports["rc_core"]:takeMoney(source,5000)
        skin = getElementModel(source) or 0
        setTimer(respawn,3000, 1, source)
        setTimer(setElementModel,3000,1,source,skin)
    end
end)

--< Respawn >--
function respawn(source)
    if isElement(source) then
        x, y, z = 2270.298828125, 1399.2269287109, 42.8203125
        repeat until spawnPlayer(source,x+1, y+1,z, 0, math.random(1,1) )
        setCameraTarget(source, player)
        fadeCamera(source, true) 
        setElementRotation(source, 0, 0, -90)
    end
end

 

Edited by TheMOG
Link to comment
4 hours ago, TheMOG said:

I'm saving player datas. The problem is, when player reconnect while dead. I want to make that, when player login, and health 0, spawning the player instant

Now i added this to the saving system

 


    if isPedDead(source) or (getElementHealth(source)) < 1 then 
        setElementHealth(source,100)
        setAccountData(account,"acc.health",100)    
    end    

 

Also, i added "if isElement(source) then ....

so, how can i make that possible, if player disconnecting while dead, the system  cancelEvent the setTimer


addEventHandler("onPlayerWasted",root,function()
    if isElement(source) then
        exports["rc_core"]:takeMoney(source,5000)
        skin = getElementModel(source) or 0
        setTimer(respawn,3000, 1, source)
        setTimer(setElementModel,3000,1,source,skin)
    end
end)

--< Respawn >--
function respawn(source)
    if isElement(source) then
        x, y, z = 2270.298828125, 1399.2269287109, 42.8203125
        repeat until spawnPlayer(source,x+1, y+1,z, 0, math.random(1,1) )
        setCameraTarget(source, player)
        fadeCamera(source, true) 
        setElementRotation(source, 0, 0, -90)
    end
end

You did not gave to function setElementModel and respawn any arguments.

Lua function usage syntax looks like: function (arg1, arg2).

And your syntax: function, arg1, arg2

Bad Syntax. You should learn lua a bit before start scripting.

 repeat until spawnPlayer(source,x+1, y+1,z, 0, math.random(1,1) )

 

Why to wipe? When fucntion inits, player will respawn "while true", e.g. infinite times. This caused network trouble, i guess. My machine is slow and if i did like you, i will need to restart my computer. :D

 

Sorry for my bad english.

Edited by f8upd8
Link to comment
6 minutes ago, TheMOG said:

But i just making a script, not opening server, is that hard to help ? Please dont comment here :)

Oh. Take it easy. It is hard to me to be gentlemen because of my basic english. I just wanted to give advice. I did not mean to be rude. I'm sorry.

Link to comment
2 hours ago, f8upd8 said:

You did not gave to function setElementModel and respawn any arguments.

Lua function usage syntax looks like: function (arg1, arg2).

And your syntax: function, arg1, arg2

Bad Syntax. You should learn lua a bit before start scripting.


 repeat until spawnPlayer(source,x+1, y+1,z, 0, math.random(1,1) )

 

Why to wipe? When fucntion inits, player will respawn "while true", e.g. infinite times. This caused network trouble, i guess. My machine is slow and if i did like you, i will need to restart my computer. :D

 

Sorry for my bad english.

What are you on about like..? How would that be infinite loop, spawnPlayer returns true, so as soon as it spawns the player, it will be terminated, but actually it's not needed anyway, since there's nothing between repeat and until - which is basically the body of the repeat function...

You should look up how stuff work before educating others, setTimer(setElementModel, 5000, 1, source, 200) is perfectly valid, since after the '1' (number of times the timer should run) you can pass arguments into the setElementModel function...

Edited by pa3ck
Link to comment
19 minutes ago, pa3ck said:

What are you on about like..? How would that be infinite loop, spawnPlayer returns true, so as soon as it spawns the player, it will be terminated, but actually it's not needed anyway, since there's nothing between repeat and until - which is basically the body of the repeat function...

You should look up how stuff work before educating others, setTimer(setElementModel, 5000, 1, source, 200) is perfectly valid, since after the '1' (number of times the timer should rund) you can pass arguments into the setElementModel function...

Wow. I did not known that. Sorry for my dumbness.

But

repeat until spawnPlayer(source,x+1, y+1,z, 0, math.random(1,1) )

Is only thing that can cause server freeze due to error. Because of invalid argument passed. I said while true and that is wrong. I should say "until false"

But even if i dumb, i had same problem and will be helpful. Try to create local variable that contains source element, i thing source don't getting through timer function.

addEventHandler("onPlayerWasted",root,function()
    local thePlayer = source
    if isElement(thePlayer) then
        exports["rc_core"]:takeMoney(thePlayer,5000)
        skin = getElementModel(thePlayer) or 0
        setTimer(respawn,3000, 1, thePlayer)
        setTimer(setElementModel,3000,1,thePlayer,skin)
    end
end)

I hope that will be just one helpful post i left here.

Edited by f8upd8
Link to comment
On ١٨‏/٧‏/٢٠١٧ at 20:38, TheMOG said:

Hello guys. I made a script, very simple onPlayerWasted respawn the player.

When i'm disconnecting while im dead, and trying to logIn, the server just froze ("NETWORK TROUBLE").

And i get these debug messages:

fsda.png

 

the screen is just black, and after a few seconds the player respawning.

Whats the problem? Here is the code:

 


addEventHandler("onPlayerJoin",root,function()
    spawn(source)
end)
--< Spawn >--
function spawn(source)
    x, y, z = 2028.4606933594, 1342.2176513672, 10.8203125
    repeat until spawnPlayer(source,x+1,y+1,z, 0,303)
    setCameraTarget(source, player)
    fadeCamera(source, true) 
    setElementRotation(source, 0, 0, -90)
end
addEventHandler("onPlayerWasted",root,function()
    exports["rc_core"]:takeMoney(source,5000)
    skin = getElementModel(source) or 0
    setTimer(respawn, 5000, 1, source)
    setTimer(setElementModel,5000,1,source,skin)
end)

--< Respawn >--
function respawn(source)
    x, y, z = 2270.298828125, 1399.2269287109, 42.8203125
    repeat until spawnPlayer(source,x+1, y+1,z, 0, math.random(1,1) )
    setCameraTarget(source, player)
    fadeCamera(source, true) 
    setElementRotation(source, 0, 0, -90)
end

 

if i understood you correctly try this code

executeSQLQuery("CREATE TABLE IF NOT EXISTS testtable (isdead TEXT, serial TEXT)")

addEventHaandler('onPlayerQuit',root,function()
  local serial=getPlayerSerial(source)
    if isPedDead(source) and serial then
  executeSQLQuery('INSERT INTO testtable VALUES(?,?)','true',serial)    
  end
  end)
addEventHandler('onPlayerLogin',root,function()
  local sql=executeSQLQuery('SELECT * FROM testtable WHERE serial=?',getPlayerSerial(source))
    if #sql~=0 then
      killPed(source)
      fadeCamera(source,false)
    setTimer(function(plr)
        local serial=getPlayerSerial(plr)     
          executeSQLQuery("DELETE FROM testtable WHERE serial=?", serial)
          fadeCamera(plr,true)
        end,1000*2,1,source)
    end
  end)

i didn't test it

Link to comment
59 minutes ago, f8upd8 said:

Wow. I did not known that. Sorry for my dumbness.

But


repeat until spawnPlayer(source,x+1, y+1,z, 0, math.random(1,1) )

Is only thing that can cause server freeze due to error. Because of invalid argument passed. I said while true and that is wrong. I should say "until false"

But even if i dumb, i had same problem and will be helpful. Try to create local variable that contains source element, i thing source don't getting through timer function.


addEventHandler("onPlayerWasted",root,function()
    local thePlayer = source
    if isElement(thePlayer) then
        exports["rc_core"]:takeMoney(thePlayer,5000)
        skin = getElementModel(thePlayer) or 0
        setTimer(respawn,3000, 1, thePlayer)
        setTimer(setElementModel,3000,1,thePlayer,skin)
    end
end)

I hope that will be just one helpful post i left here.

No, it's not like a while true do loop, it is a repeat until true - as soon as it's true, it will end.

I think the player element was the problem as well, when the setTimer fired, the source was no longer available, so declaring thePlayer as source should work. 

Link to comment

The problem is, when i die, and reconnecting, on the server console i see so many debug errors (Pictures first comment). 

I'm 100% sure, the problem is the script not checking the player is on the server because of the timer!. So,  i think i have to use cancelEvent if the player disconnect.

So, how can i make that? I never saw these errors before.

Edited by TheMOG
Link to comment
local timers = {}

addEventHandler("onPlayerWasted",root,function()
    local thePlayer = source
    if isElement(thePlayer) then
      exports["rc_core"]:takeMoney(thePlayer,5000)
      skin = getElementModel(thePlayer) or 0
      if isTimer(timers[thePlayer]) then killTimer(timers[thePlayer]) end
      timers[thePlayer] = setTimer(function() 
        respawn(thePlayer)
        setElementModel(thePlayer, skin)
      end, 3000, 1)
    end
end)
  
addEventHandler("onPlayerQuit", root, function()
      if isTimer(timers[source]) then killTimer(timers[source]) end
end)

 

  • Like 1
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...