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

What is the difference between variables and local variables?

Asked by 7 years ago
Edited 7 years ago
local variableName = script.Parent
variableNameTwo = script.Parent
while variableName.Transparency  < 1 then
        variableName.Transparency = + .1
end
while variableNameTwo.Transparency < 1 then
        variableNameTwo.Transparency = + .1
end

Would the second one not work?

1 answer

Log in to vote
0
Answered by 7 years ago

A regular variable is called a global variable, since it will work globally across the entire script. Global variables are worse on performance and should be avoided as much as possible (I have no yet found a case ware you should favor them).

On the other hand, a local variable is local to it's section of the code. Any portion of your code which is higher then the current stack, or deeper in will not be able to access the variable. For example:

local variable1 = "something"
if true then
    local variable2 = "other"
    print(variable1)
    print(variable2)
end
print(variable1)
print(variable2)

Outputs: something other something nil

Local variables are much better on performance, and generally help to avoid errors caused by variables being defined with the same name.

Ad

Answer this question