Jump to content

Checking if a player still exists


'LinKin

Recommended Posts

Hi,

I'm using a timer and passing a player as an argument;

setTimer(Func, 3000, 1, aPlayer)

So, inside the implementation of the 'Func' function, how can I check if the player that I passed to it still exists?

Would it be enough to make:

function Func(thePlayer)

if thePlayer then

code...

end

end

?

Sometimes our scripts show warnings saying things such as "Bad player 'element' pointer (1)" so if that would be the case in this script, thePlayer variable would still be true.

I was thinking about getElementType(thePlayer) == "player"

But still not sure

Link to comment

Values of variables don't change themselves, so condition evaluates to true. Using getElementType will help, but it will still output a warning message if passed element does not exist. To avoid warnings, check element's existence with isElement before checking its type.

However, even then the code is not guaranteed to work fine. If element gets destroyed and another one gets created, it's possible for new element to get the same ID (or memory address, I'm not sure what it is, but it doesn't matter) which the previous element had. If that happens, variables which held the value of previous element will start referring to the new element. So if one player quits and another one joins when the timer is still running, it's not impossible for the timer to do stuff with the new player. To guarantee that it won't happen, you need to kill the timer with killTimer when player quits instead of checking player's existence in the timer.

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