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

What does it mean to have a variable with no value or equal sign?

Asked by 7 years ago

Ive seen a lot of variables with no values and looks like this:

1local examplevariable
2local example2variable

And people use these variables as if it did have a value

Also how do people use variables with the value of nil? Is it the same thing?

1local examplevariable = nil
0
It is required in other programming languages to initialize a variable and may cause an error in the compiler. In Lua this is not required. User#5423 17 — 7y

2 answers

Log in to vote
1
Answered by 7 years ago

local examplevariable and local examplevariable = nil are essentially the same. The former is what you would call a 'variable declaration' while the latter is both a 'declaration' and 'initialization' of the variable examplevariable to nil. A place you might see this often is with a debounce variable:

01local debounce --declaration: at this point it is nil
02 
03...
04 
05--nil is a 'falsy' value
06--line 07 will evaluate to true if debounce is nil
07if not debounce then
08    debounce = true
09    print("Doing some debounced thing")
10    wait(1)
11    debounce = nil
12end
Ad
Log in to vote
0

Answer this question