Sometimes wen I look at other people's Lua code, I see the word "local" before they set a variable. What does this mean? Why do it?
When people use the keyword local before setting a variable, they create a special type of variable appropriately named a local variable.
Here's an example of such a variable.
globalVariable = 0 -- This is a global variable local localVariable = 0 -- This is a local variable [notice the keyword "local"]
One feature of local variables is that each one has a specific scope that they're limited to. Think of it as a type of specialization; local variables can only work inside of the area they specialize in.
So which scope are they limited to? In fact, what is a scope in the first place?
Local variables are limited to the scope that they are defined in. Scopes are code blocks. Code blocks are the lines of code in between control structures/statements, functions, and chunks.
Here are some examples of code blocks.
do -- This is a code block end if true then -- This is also a code block end for i = 1, 10 do -- Hey! Another code block! end while false do -- Wow, yet another! end repeat -- More code blocks! until true function codeBlock() -- Our last code block example! end
As you can see, it's pretty easy to figure out where a code block is. That being said, if you define a local variable inside of one of these code blocks, the value stored inside of that variable can only be used inside of that code block. Here's an example of that.
do local a = 3 print("a = " .. a) -- prints a = 3 end print("a = " .. a) -- prints a = nil
So why did the last print attempt print a = nil? This is because it was attempting to call the variable a inside of the current scope its in. However, you defined the local variable a inside of a scope outside of that print attempt!
So what about code blocks in code blocks? How does that affect local variables? Here's an example.
do local a = 3 do do a = 2 end end print("a = " .. a) -- prints a = 2 end
As we can see from this example, it turns out that local variables can be used in different scopes, as long as those scopes are defined in code blocks within the scope of the local variable.
Local is used in other cases to make it show that what you want is that you want the entire game to find and work.Meaning that you want something to work "online" if you know what i mean as an example.
Local players =
Local workspace =
Local humanoid =
So basically,you see what I'm trying to set here I am trying to say that a variable is assigned to local YES! you got it once you assign a variable with local it tells the script to look all over "online" in your roblox place.Meaning it wants to know that you want the "true" or "false" statement.This shows how they are used they are often also used for broken scripts hope this helped you out.
A local variable is used within a certain SCOPE, and is limited in range. People tend to prefer this in short codes when they know the SCOPE is going to be in range because Lua tends to recognize these faster (It only has to look in the scope for it, not the entire script). Global variables are preferrable in a lengthy script that you plan to use the variable in frequently throughout. Tip: You can help the script read a variable faster if you state where it ends using the ";" operator. Ex: 'local brick = script.Parent;'
To clear things up, the person who made the code used the local statement.
j = 10 -- global variable local i = 1 -- local variable
Locked by Articulating
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?