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

What is local used for? I am very confused and need examples of when you should use it.

Asked by 3 years ago

Examples are like putting a local before a function, what happens if you do it? What happens if you don’t? When should I use local in scripts?

0
local function use only in server script it will work same as local script it willmake something only for local player moneyislands1 51 — 3y

5 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

Hey Calvin,

In order to really understand the meaning of having a local in front of your variables, you need to understand how variables are stored in memory. There is a heap and a stack where variables are stored. When you declare a function local, it is stored in the stack and is scope-dependent. This means that a local variable can only be accessed within its scope. A global variable on the other hand has memory allocated to it from the heap and can be accessed anywhere in the script. So, let's move on to examples here.

Local vs. Global

global_var = "Sandwiches" -- A global variable that can be accessed throughout this whole script.

function example()
    print(global_var) -- Prints Sandwiches
    local a = "Hello World" -- This variable is accessible inside of this function and only inside this function. 
    global_var = "Burgers" -- Set it to Burgers.
    print(global_var) -- Prints Burgers
    if a == "Hello World" then
        local b = 3 -- A variable that can only be accessed within this if statement, since that is the new scope.
    end

    print(a, b) -- Should print: Hello World nil    

    local function local_func() -- A function that can only be called within this function
        print("Local function ran.")
    end
end 

print(global_var) -- Prints Sandwiches
example() -- Initiates the above function. 
print(global_var) -- Prints Burgers
local_func()  --  Throws an error "attempt to call a nil value"

As a general rule, local variables are better to use since they allow the variable to be stored only when it is relevant. Global variables are frowned upon usually since they do not follow common coding practice and generally tend to cause lag in the long run.

Here are some resources I recommend.

Stack vs. Heap

Scopes

Local variables vs. Global variables

Thanks,

Best regards,

IdealistDeveloper

Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

There mostly isn't any difference in putting local and no local before a variable.

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

print(b) --it'll print out 2

.

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

print(b) --it'll print out nil

As stated in the Variables Website here,

Local variables are obtained faster than global variables because they're integrated into the environment in which they were created. If possible, you should always use local variables over global variables, unless there's a specific reason otherwise.

0
Scopes is one way to counter your argument, as well as scenarios of debugging and using setfenv and getfenv greatneil80 2647 — 3y
0
this doesn't explain why b will be nil, make your answer explain scopes and local vs global ayuu1234 0 — 3y
Log in to vote
0
Answered by 3 years ago

To understand this we have to go into the two types of publicity for variables Local vs Global

A local variable is a variable only accessible by the script or function that it is in, Globals can be accessed from anywhere, to make a global you simply just don't put local in front of a variable the script I have below should show you what I mean

function urdad(e, h)
    --var 1 cheese is local
    local cheese = "Cheese"
    --var 2 potassium is NOT local
    potassium = banana
end

local function urmom()
    print(cheese)
    -- the above won't work since cheese is local to the urdad function and can only be called from there
    print(potassium)
    --this will work because potassium is global and can chance be called from anywhere
end

The same applies for functions too, like if I was in a for loop and I made a local function I could only call it from the for loop

I hope that cleared up your doubts!

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Its a variable, a way to refer to something stored in a variable

game.Workspace.Door.LocalScript.Disabled = true

but with local u can do this

local Door = game.Workspace.Door.LocalScript

Door.Disabled = true

wait(1)

Door.Disabled = false

its very useful when you want to refer to something multiple times

Log in to vote
-1
Answered by 3 years ago

The function local is the same as var. If you use Javascript or any other coding, local is a way to make a variable. This can store data and can be changed. For example, ~~~~~~~~~~~~~~~~~

local Baseplate = game.Workspace.Baseplate Baseplate:Destroy() ~~~~~~~~~~~~~~~~~

Using a variable is easier to get data faster, or storing some

0
this isnt near of a real explanation TheEagle722 170 — 3y
0
This doesn't answer his question ayuu1234 0 — 3y
0
this isn't an explanation nor an answer first of all, it's gibberish put together by a rookie programmer dealt in a childish explanation. greatneil80 2647 — 3y
0
yea! basically a variable sonichfan 62 — 3y

Answer this question