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 6 years ago

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

local examplevariable 
local 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?

local 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 — 6y

2 answers

Log in to vote
1
Answered by 6 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:

local debounce --declaration: at this point it is nil

...

--nil is a 'falsy' value
--line 07 will evaluate to true if debounce is nil
if not debounce then
    debounce = true
    print("Doing some debounced thing")
    wait(1)
    debounce = nil
end
Ad
Log in to vote
0

Answer this question