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

Why use 'local' before my variables declarations?

Asked by 4 years ago

I've read every article I could find about the use of the 'local' prefix before declaring a variable and I can understand its usage when present before a variable declared inside a function but is there any use on using said prefix on variables declared at the start of my script?

Like in the following example, does having my 'part' variable be a local one is useful in any way? Or should I just leave it as a global one like 'part2'?

local part = "Part"
part2 = "Part2"

function printParts()
    print(part)
    print(part2)
end

printParts() -- works fine with both variables
1
I'm not sure if I'm correct, but I remember before that using local variables at the start of your script slightly improves your script's performance. Other than that though, it doesn't really do much. User#20279 0 — 4y

4 answers

Log in to vote
2
Answered by 4 years ago
Edited 4 years ago

The "local" keyword simply localizes the variable to the register in context. Accessing values in memory from the local register tends to be faster since it's "closer" than a global.

The instructions below represent adding the value of global b to global a.

GETGLOBAL       0 0         ; a
GETGLOBAL       1 1         ; b
ADD             0 0 1   
SETGLOBAL       0 0         ; a
0
So other than that there's no reason I should be using the local prefix on my variables declared at the start of my script? I'm asking because I fear I will soon hit the 200 local variables limit on my old script and modularizing it at this point would take me so much more time than just removing a couple local prefixes from my variables. Le_Teapots 913 — 4y
1
@tiraner300 Yeah, it's considered good practice localize all your scope variables since it makes them a bit faster, but as you've figured out, the register can only hold so many variables before it gets full, in which case you can just use globals. In the end it really doesn't make that much of a difference though. davidgingerich 603 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

Please refer yourself to this handy guide by me on how the difference between local and not local variables work:

The Youtube Video I Made

Subscribe and liked if it helped! ;)

Otherwise if that video does not help after you watched it fully, and maybe took the model or if u didnt want to watch it:

Local means it only works in that code area such as:

function onClicked()
local test = nil -- as u can see if its local it only works in a part of code ended by a end like this, not just a function but..
end

test = false -- u cannot call on the variable outside of the code area.

This happens most of the time, and it's very little like 0.1% for a local variable to work outside of the code area. a local variable usually does not need a local script.

a non-local variable means it can be used anywhere in the script.

0
Thanks! Even tho my question was not exactly about the core functionality of the local prefix, I appreciate the detailed explanation. Le_Teapots 913 — 4y
1
"a non-local variable means it can be used anywhere in the script" is wrong. If you set a local variable at the base scope (the first scope), it can be accessed anywhere in the script. DeceptiveCaster 3761 — 4y
Log in to vote
1
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago

Local variables basically means it happens in its own scope. E.g. we could have 2 parts in our script and we only wanted it to happen in one of the part then we use local variables and plus. Hoped that helps. For more explanations check the wiki or

Check peaspod.

Log in to vote
-1
Answered by 4 years ago

local scripts are stupid and useles, never use them bro caus if u use a local script u have to use a remot event and a server script too but if u do all inside the server script u dont need no remote and no local script so yeah they are completly pointless

0
They aren't. they can be used for UI Things. and things that only require the local player. RobloxGameingStudios 145 — 4y

Answer this question