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
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
Please refer yourself to this handy guide by me on how the difference between local and not local variables work:
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.
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.
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