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

01global_var = "Sandwiches" -- A global variable that can be accessed throughout this whole script.
02 
03function example()
04    print(global_var) -- Prints Sandwiches
05    local a = "Hello World" -- This variable is accessible inside of this function and only inside this function.
06    global_var = "Burgers" -- Set it to Burgers.
07    print(global_var) -- Prints Burgers
08    if a == "Hello World" then
09        local b = 3 -- A variable that can only be accessed within this if statement, since that is the new scope.
10    end
11 
12    print(a, b) -- Should print: Hello World nil   
13 
14    local function local_func() -- A function that can only be called within this function
15        print("Local function ran.")
View all 22 lines...

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.

1local a = 1
2if a == 1 then
3    b = 2
4end
5 
6print(b) --it'll print out 2

.

1local a = 1
2if a == 1 then
3    local b = 2
4end
5 
6print(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

01function urdad(e, h)
02    --var 1 cheese is local
03    local cheese = "Cheese"
04    --var 2 potassium is NOT local
05    potassium = banana
06end
07 
08local function urmom()
09    print(cheese)
10    -- the above won't work since cheese is local to the urdad function and can only be called from there
11    print(potassium)
12    --this will work because potassium is global and can chance be called from anywhere
13end

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

1game.Workspace.Door.LocalScript.Disabled = true

but with local u can do this

1local Door = game.Workspace.Door.LocalScript
2 
3Door.Disabled = true
4 
5wait(1)
6 
7Door.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