Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

If local variable doesn't exist then don't do anything?

Asked by 5 years ago

Hi,

I have a local variable in my script that can sometimes be turned off or in other terms stop existing (it's hard to explain). I'm referencing this local variable, however, when it stops existing my if statement referencing the local variable breaks the script. Is there a way to make my script check if the particular local variable still exists prior to running the command?

Thanks.

1
Do you have code to show? DeceptiveCaster 3761 — 5y
0
if variable ~= nil then end User#24403 69 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago

simply test for it's existence, if you use a variable that returns nil, it most likely does not exist, also, nil can also represent false in lua

if -variable- then
    --do stuff
end

replace -variable- with the variable to test

0
"nil can also represent false" No it can't. The value nil is not equal to false. It's referred to as "falsey", which is not the same as "false". DeceptiveCaster 3761 — 4y
Ad
Log in to vote
0
Answered by
sleazel 1287 Moderation Voter
4 years ago

You should rewrite your script, so its always is prepared for any conditions. If you really, i mean REALLY can't, use ypcall:

ypcall(function()   

    --put any dangerous code here that may crash your script.

end)

Since you did not show us your script, I can't be certain that this solution is right for you.

Answer this question