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

What does the "local" word exactly do?

Asked by 6 years ago

Hello, i'm new at Roblox's lua scripting system and also on lua scripting itself. You will be seing me on this forums quite a lot as i have lots of doubts.

So, incase i want to create a function, what will change if i put "local function" or "function" ? I will be thankful for any help :)

3 answers

Log in to vote
4
Answered by 6 years ago
Edited 6 years ago

Summary The term "local" within the lua language is used to define a variable that is not global, and that will be used within the enclosing code/function. This is explained in further detail below.

Definition Local is defined as "Pertaining to or characterized by place or position in space." My source is dictionary.com. "Pertaining to ..." is very important because it shows how variables defined as local within lua pertain the enclosing code/function.

Example Here is an example with an explanation:

local helloworld = "Hello World!";

function printRandomText()
    local texts = {"hi", helloworld, "how's your day?"}; -- Here we LOCALLY define 'texts'
    local number = math.random(1,#texts); -- Here we LOCALLY define 'number'
    print(texts[number]);
end

-- If I were to attempt to print the variable 'texts' or 'number' we wouldn't receive an error although 'nil' would be printed instead.
print(number); -- This will print 'nil'

Other Languages In other coding/scripting languages you'll notice local most likely won't exist. Most languages have their alternative to local variables.

Conclusion The term "local" is very important within the Lua language and it's always best practice to use it if you're not going to be defining a global variable. I hope I helped!

0
"local" can be a shortcut for a word such as: local Gorilla = script.Parent, you can use it to make shortcut words for lines of code MrDragonTaker 0 — 6y
0
Variables are shortcuts..... @MrDragonTaker AstrealDev 728 — 6y
Ad
Log in to vote
1
Answered by
Asceylos 562 Moderation Voter
6 years ago
Edited 6 years ago

Local means that something only exists within a function. So if you have a function inside a function and you only want to use it inside the named function, you make it local. If you want to be able to use it outside of the named function, you just leave away the "local" although you could just place it outside the named function then.

Log in to vote
1
Answered by
movsb 242 Moderation Voter
6 years ago

In Lua, the local keyword means that the given variable is to be stored in a virtual register, rather than the global environment table.

Furthermore, a local variable is limited to the scope that it is declared in, which is why we use them in functions. In Lua it is good practice to always use local variables, because in the long run, locals can be accessed up to 30% faster than globals (due to the fact that globals exist inside of a table that must be iterated over to find the global)

https://stackoverflow.com/questions/9132288/why-are-local-variables-accessed-faster-than-global-variables-in-lua

If you were running code in a Lua shell, you would find out that when you declare a local variable in one line then execute the line, then try to reference that local variable from the new line you will be returned nil. Each line you execute in the shell has its own scope.

In C, variables stored on the stack are local by default. Consider the following C function:

void my_func(void) {
    int local_integer = 123;
}

when the function my_func returns to the instruction after the call to it, the variable local_integer will no longer exist because it is limited to my_func's scope. All local variables from a functions scope are de-allocated before the function returns.

This is relatively the same case for Lua.

Answer this question