|
| 21 Jul 2014 07:19 PM |
My friend tells me that local before a variable tells Lua that you are declaring a variable. But it still works the same if you don't put the word local before you declare a variable...
EXAMPLE:
local var = 1 or var = 1
what difference does it make, and what other scripts can I use it for. Please help! I tried searching on google but nothing really came up. |
|
|
| Report Abuse |
|
|
HuntHello
|
  |
| Joined: 19 Jan 2011 |
| Total Posts: 577 |
|
|
| 21 Jul 2014 07:22 PM |
| if you declare a local variable its only available in the current scope. |
|
|
| Report Abuse |
|
|
|
| 21 Jul 2014 07:25 PM |
| current scope? Like the script only? |
|
|
| Report Abuse |
|
|
HuntHello
|
  |
| Joined: 19 Jan 2011 |
| Total Posts: 577 |
|
|
| 21 Jul 2014 08:59 PM |
hello, in the script: local a = 1 print(a) function hi() print(a) local b = 2 end hi() print(b) , the output will be 1 1 nil this is because declaring a local variable in a function will only work in the function, so variable b exists in the function, but not outside of it.
|
|
|
| Report Abuse |
|
|
HuntHello
|
  |
| Joined: 19 Jan 2011 |
| Total Posts: 577 |
|
|
| 21 Jul 2014 09:01 PM |
| for added reference, if you want a variable that can be referenced outside of the script (called a global function) take a look at _G variables, http://wiki.roblox.com/index.php?title=Global_function |
|
|
| Report Abuse |
|
|
Xephyric
|
  |
| Joined: 26 Mar 2009 |
| Total Posts: 2270 |
|
|
| 21 Jul 2014 09:03 PM |
if blah then --this is a scope end if blah then --this is another scope end
if blah then --this is a scope if blah then --this is a scope that derives from that one (and can use it's local variables) end
|
|
|
| Report Abuse |
|
|
Xephyric
|
  |
| Joined: 26 Mar 2009 |
| Total Posts: 2270 |
|
| |
|
|
| 21 Jul 2014 09:06 PM |
a = true do local a = false print(a)--false end print(a)--true
local variables can only be used inside their 'scope' |
|
|
| Report Abuse |
|
|
| |
|