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

"local" in front of variables [closed]

Asked by 10 years ago

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?

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?

4 answers

Log in to vote
9
Answered by
Unclear 1776 Moderation Voter
10 years ago

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.

4
This is an excellent answer. I think it would be useful to indicate that another feature of local variables is that they are accessed more quickly than global variables. Since global variables cannot be used outside of the declaring script in ROBLOX anyway, there is no reason to use globals at any time. AxeOfMen 434 — 10y
Ad
Log in to vote
-2
Answered by 10 years ago

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.

Log in to vote
-2
Answered by
Maxomega3 106
10 years ago

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;'

1
Why did I lose points for this answer? Maxomega3 106 — 10y
Log in to vote
-7
Answered by 10 years ago

To clear things up, the person who made the code used the local statement.

  j = 10         -- global variable
    local i = 1    -- local variable