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

[SOLVED] What is the point of making variable "local"? (Read description)

Asked by 5 years ago
Edited 5 years ago

Hey guys, today a simple question.

What is the point of making a "local" variable, when you can't use a variable in other scripts even if it global?

I know it might sound as a noob question, but I will be happy for any explanation, or an answer.

Thanks in advance!

2
I believe it has to do with scopes ForeverBrown 356 — 5y
2
also, I think they are faster to read from when in the same scope as the thing calling it ForeverBrown 356 — 5y
1
brownpants01 is correct about them being faster (I think to read from and write to) chess123mate 5873 — 5y

1 answer

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

A "local" Variable, is restricted to the enclosing function, If you wanted to not get confused with all your variables its quite useful.

Jim = "Jim"

function example()
    local Bob = "Bob"
    print(Bob)-- Prints "Bob"
    print(Jim)-- Prints "Jim"
end
example()
print(Jim) -- Prints "Jim"
print(Bob) -- Errors
0
Please accept the answer if this helped :) UltraaaDev 85 — 5y
0
Line 10 would not error. It would print nil. Amiaa16 3227 — 5y
0
it also means that you can't share it through scripts, like _G Time_URSS 146 — 5y
0
Wrong. local var = "hi"; _G.var = var; <-- what's stopping you from doing that @Time_URSS? Amiaa16 3227 — 5y
1
not just a function, any scope. loops, do blocks, if statements, etc User#22604 1 — 5y
Ad

Answer this question