Usage of local variables in outermost scope

I’m new to Roblox Lua but I have experience in other programming languages. I found many tutorials on the web, and all of the tutorials use local to create variables – in the outermost scope. For example:

local someVariable = "foo"
local someVariableAgain = "bar"

function someFunction()
    -- some code here
end

local in the other programming languages I used, are used to hide variables from other scopes, so you won’t accidentally overwrite them. I’m wondering now, what is the usage of local in the outermost scope. I searched the web, but can’t find any info. They don’t seem to be a required syntax of Lua when creating variables, because someVariable = "foo" seems to work fine in the Roblox Studio command bar, and in scripts too. Can someone bring up a reasonable explanation?

2 Likes

local to be used globally in a script is generally up to preference from what I infer. It isn’t necessary to use it in the outermost scope. local only really serves a real purpose when using it in lower scopes, particularly within functions.

From what I’ve read on a thread on scriptinghelpers, accessing local variables are faster than accessing global variables when used in the outermost scope. Mind you, this is only from what I read, I don’t know how true this is or not.

2 Likes

Oh wow, I didn’t know that. I found something after googling ‘global and local speed lua’ and found this stackoverflow post. It says:

The difference in running time is due to the difference between hash table lookup and array lookup. An interpreter might be able to place a local variable in a CPU register, but even without such cleverness local variables are faster to access.

Global variables in Lua are stored in tables. Generally, anyone can modify these tables, and therefore the interpreter has to lookup a value anew every time it is being accessed. Local variables on the other hand disappear only when they go out of scope. Therefore they can have fixed locations in an array.

Looks like it is indeed true :smile:

1 Like

I’ve read many topics on this issue, and from what I have gathered the difference is not much.

The primary difference is that supposedly if you do not add the local, it will exist in the servers memory forever until the server closes even if the script is destroyed or in no other way running.

I am not 100% about this, and even if it is true I highly doubt it would ever have a noticeable impact on the game unless maybe you had tens of thousands of variables defined.

Something interesting I found on the Variable page of the Dev Hub was this;

Edit: This seems to support what you and @DesiredFlamingFire mentioned.

1 Like

Huh, ‘intergrated into the environment in which they were created’. Roblox wants us to ‘always use local variables over global variables, unless there’s a specific reason otherwise’.

I have been thinking that global variables are completely useless. My idea:

Global Approach

for i = 1, 10 do -- this loop is stupid, but for demonstration purposes :)
    someVar = i
end
print(someVar) -- prints 10

(why not just use this)
Local Approach

local someVar
for i = 1, 10 do
    someVar = i
end
print(someVar) -- also prints 10

Disclaimer: Global variables are completely useless in Lua

1 Like

Yeah, I really wonder what they meant by “unless there’s a specific reason otherwise”. I can’t think of anything off the top of my head.

1 Like

Lua globals just aren’t very useful in Roblox Lua. You should always be using locals, and the reasons you’d have to use globals usually hinge on properties of vanilla Lua environments where the entire program shares an environment and it’s not just separated by script.

getfenv and setfenv only access and affect global variables. For example:

local x = 3
y = 4

for i, v in pairs(getfenv()) do
	print(i, ": ", v)
end

This will print that y is 4, but it won’t find x at all. However, you should try not to use getfenv and setfenv if you can, since they disable some Luau optimizations.

5 Likes