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

What are local variables?

Ok, I know this is not a question about scripting directly,

but I just would like to know the difference between local and normal variables. It would help my scripting experience a lot.

2 answers

Local variables have a more restricted scope than "normal" (global) variables.

Most of the time, in good code, you are happy when everything is local.

local variables are not "visible" (they cannot be read from or written to) outside of the definition of a function. Consider the following:


function withLocal()
	local a = 5;
	print("a =",a);
end
a = 3;
withLocal();
print("a =",a);
-- Output:
-- a = 5
-- a = 3

function withoutLocal()
	a = 5;
	print("a =",a);
end
a = 3;
withoutLocal();
print("a =",a);
-- Output:
-- a = 5
-- a = 5

When the variable is local, it can only be seen within the function that it was defined. Because this makes it much clearer what can modify and be affected by it, you almost always want this. It is also imperative to have any useful results in recursive methods.

Gist: Difference between global/local is sometimes subtle. Local inside of function means that value doesn't exist outside. When there is a difference, you almost always want the local version in clean, modular code.

1 comment

0

Thanks! Helped alot.

So as far as variables go, there are 2 main types: global variables and local variables. Global variables are variables that can be accessed in any scope in a script. Here is an example:

x = 1 --tells the script that x is a global variable for 1
if x == 1 then --if x is equal to 1 then
	print("x is equal to 1") --prints 'x is equal to 1'
end

Local variables are variables that are accessed in the scope it is defined in. You could use a local variable like this:

if true then --automatic if statement
	local x = 2 --defines the local variable
	print(x) --outputs 2 
end

Local variables can also be tricky because if a variable is defined in a larger scope, it can be used in a smaller scope, but anything that the smaller scope changes about the variable will not be carried into the larger scope. Look at this for an example:

x = 10 --defines variable in larger scope (in this case, the scope is the script)
if true then
	local x = 1 --calls for a local x to be 1
	print(x) --prints the local x
end
print(x) --prints the global/outer x

In this script example, the global variable x is defined as 10. Inside the if scope, the script calls for a x to be a new variable that is local just for that scope. On the inside, x is 1, but on the outside x is still 10.

You can think of local variables in scopes like a law in a state versus a law in a country (global variables). States can decide certain laws to be a certain way in that state, but only in that state. Outside of that state, other laws may apply.

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