Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

2 comments

1

Do you have code to show?

0

if variable ~= nil then end

2 answers

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

1 comment

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

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.