|
| 26 Jul 2017 12:55 PM |
What's the difference between, say...
brick = script.Parent
and
local brick = script.Parent
Does the 'local' variable make that variable only usable for the script it's define in or something? |
|
|
| Report Abuse |
|
|
|
| 26 Jul 2017 12:57 PM |
the difference is
brick = script.Parent
function doStuff() brick = something end
this will work
local brick = script.Parent
function doStuff() brick = something end
this will not work
local vars only exist within the scope that they're set in
|
|
|
| Report Abuse |
|
|
|
| 26 Jul 2017 01:03 PM |
When using "local" it is protecting the variable. Meaning:
If I do this:
brick.Transparency = 0.5 brick = script.Parent
The transparency will be applied because the variable is public. The parser knows what "brick" is in this instance so therefore, it applies the transparency.
However, if I do:
brick.Transparency = 0.5 local brick = script.Parent
The transparency will not be applied and the script will error saying "brick" is non-existent.
Also, "local" can be used to make variables function/iterator protected.
Like so:
function Func1() local brick = script.Parent end
Func1()
brick.Transparency = 0.5
The script will error out saying brick doesn't exist because "local" makes brick only accessible within the function and sub-functions.
local can also be applied to functions.
Func1() local function Func1() print("hello") end
This will error out because "Func1()" is called before the function is created.
Another thing is that not applying "local" to variables within functions/iterators also makes them public; however, calling "brick" before the function will error out because "brick" isn't created until the function is called.
Sometimes, it's the people no one imagines anything of, who do the things that no one can imagine. |
|
|
| Report Abuse |
|
|
LaeMVP
|
  |
| Joined: 24 Jun 2013 |
| Total Posts: 4416 |
|
|
| 26 Jul 2017 01:08 PM |
| tldr, it has something to do with scope |
|
|
| Report Abuse |
|
|
Tynezz
|
  |
| Joined: 28 Apr 2014 |
| Total Posts: 4945 |
|
|
| 26 Jul 2017 01:09 PM |
local are like house rules, they only apply to your house nobody else laws apply to everyone
laws='dont murder'
function house1() local house1rule='no phones' print(house1rule) --> no phones print(laws) --> dont murder end
function house2() local house2rule='no music' print(house2rule) --> no music print(laws) --> dont murder print(house1rule) --> nil, their house rules dont apply to us end
house1() house2() |
|
|
| Report Abuse |
|
|