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

What is a Scope?

Asked by 7 years ago
Edited 7 years ago

Hey guys, so I was checking on the Roblox wiki for local variables. All of a sudden it started talking about scopes. I looked up what a scope is on the internet. It told me that it's similar to a variable. It can be used to refer to an entity. However, then I didn't know what Roblox wiki meant by Local Variables being used in a Scope. So, I came here for my last resort. Anyways, let me quote what Roblox wiki tells you about local variables in Scopes.

Here it is:

"A local variable is a variable that can only be accessed by other things in the same scope. A variable is made local by adding the keyword local before the variable. It is true that local variables are obtained faster than normal variables. This is because they are integrated into the current environment they were created in."

Anyways, I want the most easiest to understand description of what a Scope is. Also, I want a description of what Roblox would use this for and how Roblox addresses a Scope.

Well, thank you for reading this question and any help is helpful.

~~ KingLoneCat

2 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
7 years ago
Edited 7 years ago

The Glossary on SH defines many terms! If it's missing a term or needs a better explanation, please leave a Feedback (with the tab floating on the left side of the page) and I will pay it more attention!

I wrote the scope entry a few weeks ago.

Scope

The scope of a name is the places in a script that a name has meaning.

For local variables, scopes begin at definition and end at the end of the block the local was defined in (at the corresponding end or until keyword).

Not-local variables have the scope of the entire script.

Function parameters have the scope of the function they are defined in. ... cannot be used in functions defined with another function.

Variables defined in a then or else are not "visible" after the if ends.

Example

-- The comments on each line indicate which variables are "in scope"
-- at that line.
local A = 5 -- (none)
-- A
local G = 6 -- A
-- AG
local function f(B)
    -- ABGf
    local x = B -- ABGf
    -- ABGfx
    if x then
        -- ABGfx
        local y = x -- ABGfx
        -- ABGfxy
    else
        -- ABGfx
        local z = x -- ABGfx
        -- ABGfxz
    end
    -- ABGfx
end
-- AGf

Notice: + local variables are only in-scope after being declared + when a block closes (e.g., at an end), all the local variables that were defined in that block disappear + when a block closes, all the local variables defined before that block are still around

Subtleties of Scope

Variables defined in a repeat loop can be used in the condition of the until but cannot be used after the loop.

Local variables cannot be used in the statement that defines them; this can pose a problem for connections and other anonymous functions:

local con = part.Touched:connect(function()
    con:disconnect() -- NO! `con`'s scope does not include its definition
end)

You have to break this up into two statements:

local con
con = part.Touched:connect(function()
    con:disconnect()
end)

Because blocks of statements mark the possible boundaries of scopes, they are often referred to as scopes. Every script has a global scope for all non-local variables.

Each block gets one level of indentation. See Indentation.

Ad
Log in to vote
0
Answered by
funyun 958 Moderation Voter
7 years ago
Edited 7 years ago
number = 5 --This is a variable in the global scope. It can be accessed...

if 2 + 2 == 4 then
    print(number) --Here
end

for i = 1, 10 do
    print(number) --Here
end

function f()
    if 2 + 2 == 4 then
        for i = 1, 10 do
            print(number) --And here too.
        end
    end
end

f()

if 2 + 2 == 4 then
    if 1 + 1 == 2 then
        local othernumber = 10 --This is a local variable. It can only be 
        --accessed from within its own scope. That is...
        print(othernumber) --Here
        for i = 1, 3 do
            print(othernumber) --And here
        end
    end
    print(othernumber) --But not here.
end

print(othernumber) --Not here either

--Notice how tabbing outlines the scope. Functions, if statements, and loops get their own scopes.
--Localizing a variable could mark the difference between this:

if 1 + 1 == 2 then
    a = 1 --Global variable, can be accessed outside of current scope
end

print(a) --Will work

--And this

if 1 + 1 == 2 then
    local a = 1 --Local variable, can't be accessed outside of current scope
end

print(a) --Will not work

--It can also mark the difference between this:

a = 1

if 1 + 1 == 2 then
    a = 2
end

print(a) --2, since we changed the value of the global variable in the scope above

--and this:

a = 1

if 1 + 1 == 2 then
    local a = 2
end

print(a) --1, since the local and global variables are not the same

Answer this question