Mmoniot
|
  |
| Joined: 12 Mar 2012 |
| Total Posts: 85 |
|
|
| 18 Jan 2015 05:25 PM |
If you were to write something like;
Bin = script.Parent
at the TOP of the script(outside of a function), then what is the precise difference from writing;
local Bin = script.Parent
They seem to work the same, but I do not know if one is better |
|
|
| Report Abuse |
|
|
|
| 18 Jan 2015 05:25 PM |
i mean
they're pretty much the same in that case
but i always use local, because it looks neater |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 18 Jan 2015 05:26 PM |
| dont always use local, there are some times when you dont want to |
|
|
| Report Abuse |
|
|
|
| 18 Jan 2015 05:26 PM |
@chim
no
you can ALWAYS use local |
|
|
| Report Abuse |
|
|
|
| 18 Jan 2015 05:27 PM |
local is like an inside joke
anyone outside of the function that u make it in
wont get the joke and will break ur script in retaliation |
|
|
| Report Abuse |
|
|
amanda
|
  |
| Joined: 21 Nov 2006 |
| Total Posts: 5925 |
|
|
| 18 Jan 2015 05:29 PM |
If you ever want to use the word Bin again.
For example, if you wanted a script you could put into any part
to be
part = script.Parent
it wouldn't work for more than one because part only gets defined once
but if you did
local part = script.Parent
you could do that in as many scripts as you want because it is local to the script |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
|
| 18 Jan 2015 05:30 PM |
nope dumb@$$
--- local chim = "CHIMMIHC"
function nope() if chim == "CHIMMIHC" then local chim = "hi" end print(chim) end
nope()
--> CHIMMIHC
----
local chim = "CHIMMIHC"
function nope() if chim == "CHIMMIHC" then chim = "hi" end print(chim) end
nope()
--> hi
|
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
| |
|
|
| 18 Jan 2015 05:33 PM |
@chim
there is always a way around it, just don't name it the same thing, lol
local test = "var"
function nope() local thing; if test == "var" then thing = "dumbass" end print(thing or "test isn't equal to var!") end
nope()
and i didn't mention that your script changes the original chim on line one ;0 |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 18 Jan 2015 05:33 PM |
chim, you do realize there is a difference between declaring a global variable and just re-setting a local/global variable.
local x = 5; x = 10;
There, x = 10 will set the local variable x to 10, not define a global one. Your example doesn't help the argument at all. |
|
|
| Report Abuse |
|
|
chimmihc
|
  |
| Joined: 01 Sep 2014 |
| Total Posts: 17143 |
|
| |
|
Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
|
| 18 Jan 2015 05:44 PM |
Local variables are 'local' to the scope that you set them in.
local derp = 0 --This would be local to the outer scope so it doesn't really matter in this scenario.
local derp = 0
function hi() local other = derp + 1 print(other) --> 1 end
print(other) --> nil
--'other' is local to the scope of the function, if you attempt to use the variable outside the scope it will return nil. |
|
|
| Report Abuse |
|
|
Mmoniot
|
  |
| Joined: 12 Mar 2012 |
| Total Posts: 85 |
|
|
| 18 Jan 2015 06:41 PM |
| I understand local variables, I just want to know if it makes any difference setting a variable to local outside of a function |
|
|
| Report Abuse |
|
|
Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
| |
|
|
| 18 Jan 2015 07:01 PM |
| Local cannot be accessed out of a scope. |
|
|
| Report Abuse |
|
|
LucasLua
|
  |
| Joined: 18 Jun 2008 |
| Total Posts: 7386 |
|
|
| 18 Jan 2015 07:02 PM |
| I can't tell if chim is trolling or missing the point entirely... |
|
|
| Report Abuse |
|
|
|
| 18 Jan 2015 07:03 PM |
@Lucas Chim is a beginner at scripting. |
|
|
| Report Abuse |
|
|
Goulstem
|
  |
| Joined: 04 Jul 2012 |
| Total Posts: 7177 |
|
|
| 18 Jan 2015 07:09 PM |
@joel
No he's not. He's like intermediate level, probably better than you. |
|
|
| Report Abuse |
|
|
|
| 18 Jan 2015 07:14 PM |
| ah goulstem why do you have me all of a sudden, we used 2 b frends |
|
|
| Report Abuse |
|
|
Voiliax
|
  |
| Joined: 05 Nov 2009 |
| Total Posts: 15554 |
|
|
| 18 Jan 2015 07:32 PM |
Always use local variables, it's better and overall more efficient. When you set/access a global variable, you're actually setting/accessing a table index, the environment table. Whilst local variables behave differently and does not require as much performance compared to global variables.
If you want a local variable to behave like a "global" variable, set it as an upvalue. Upvalue only means an external local value. A local variable that is set at the script's scope.
e.g.:
local lol = "Hi" local lol2
do lol2 = "Hi2" -- lol2 is still a local variaible at the script's scope (an upvalue) end
print(lol2) |
|
|
| Report Abuse |
|
|
|
| 18 Jan 2015 08:23 PM |
http://wiki.roblox.com/index.php?title=Local_variable
Wow guys, wow! |
|
|
| Report Abuse |
|
|