cntkillme
#180964069Sunday, January 03, 2016 3:48 AM GMT

It would make it slightly slower because loops (and functions) have slight overhead. If you have to go deep enough, you'll run out of local variable space (you can have a maximum of 255 (well 250 to be accurate))
izzatnasruna
#180964089Sunday, January 03, 2016 3:48 AM GMT

Is this just for RBX.Lua or just the general Lua? If I remember correctly, about the _G, ServerScripts and LocalScripts do not have the same _G content.
cntkillme
#180964124Sunday, January 03, 2016 3:49 AM GMT

maximum of 250 per function* (forgot that part)
cntkillme
#180964195Sunday, January 03, 2016 3:50 AM GMT

A lot of this applies to regular Lua. Only #5 and #7 are Roblox-specific
dennis96411
#180964426Sunday, January 03, 2016 3:52 AM GMT

Is there any downside to doing that though?
cntkillme
#180964498Sunday, January 03, 2016 3:53 AM GMT

Well it's harder to read IMO and (probably insignificantly) slower to nest loops if you can use a single loop. But if it works for you and it looks good to you, you might as well keep doing it.
UnstableScript0
#180964612Sunday, January 03, 2016 3:55 AM GMT

I have seen several people who think this, so I will mention it: ModuleScripts are more efficient. The above is not true. It actually takes more memory and stuff because you have to call require(), pass variables, etc. When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
dennis96411
#180964832Sunday, January 03, 2016 3:58 AM GMT

No, I'm talking about declaring global functions as local in the beginning of the script. Like doing this: --Line 1 local table_insert = table.insert --Somewhere past line 1000 ... ... ... ... table_insert(Table, Something) ... ... ... ...
izzatnasruna
#180964959Sunday, January 03, 2016 3:59 AM GMT

@dennis96411 But, why would you do that? Isn't table.insert is easier to type rather than table_insert ?
cntkillme
#180965050Sunday, January 03, 2016 4:00 AM GMT

Ohh, okay in which case that's where upvalues come into play. Nesting blocks (not including functions) will not change anything but if you were to do: local x = table.insert local function blah() -- use "x" here end It would be slightly slower to access x because it's in the caller's callstack, not in blah's, but it's still much faster to access than a global variable.
dennis96411
#180965395Sunday, January 03, 2016 4:05 AM GMT

So if I'm understanding this correctly, when you index a variable in the environment, it will try to find it in the local stack, then keep going up through each caller's stack until it reaches the global?
cntkillme
#180965615Sunday, January 03, 2016 4:08 AM GMT

Not really. The compiler actually sorts this mess out. When you have something like this: local x = 5; do local y = 10; end local z = 15; The compiler will spit out something like this: LOADK 0, // x LOADK 1, // y LOADK 1, The first register will be reused because the compiler has determined that it was in a block of its own. And this code will generate the _same_ thing: local x = 5; local y = 10; y = 15; However if the compiler sees that there are no local variables, it will just go straight to global.
cntkillme
#180965689Sunday, January 03, 2016 4:09 AM GMT

"block of its own" as in, after that point it won't ever be used again (since well, that's the point of local variables) so it reuses the register**
LongKillKreations
#180965771Sunday, January 03, 2016 4:10 AM GMT

Thank you.
cntkillme
#180966169Sunday, January 03, 2016 4:15 AM GMT

Anyways, to quickly answer your question since I don't think I actually did: local x = blah; do do do do -- x stuff end end end end Accessing x up there is no difference than accessing x below: local x = blah -- x stuff Unless x is outside a function (which in this case there are none).
dennis96411
#180966316Sunday, January 03, 2016 4:17 AM GMT

If those were functions, then how would it be different?
cntkillme
#180966454Sunday, January 03, 2016 4:19 AM GMT

They would be upvalues to that function, as an example: local x = blah local function abc() local function def() -- do stuff with x end end "x" does not exist in those functions' stack by default, but they exist outside of it (locals outside a function are upvalues to that function). So instead of "getting a local" it would have to "get an upvalue" (which is still really fast, much faster than getting a global).
dennis96411
#180966731Sunday, January 03, 2016 4:23 AM GMT

So if I'm doing a lot of long operations with those global functions, given that I have enough space on that stack, I should declare them as local?
cntkillme
#180966882Sunday, January 03, 2016 4:25 AM GMT

Well if speed is super vital, since accessing a global requires a Lua table lookup whereas accessing a local requires just a simple C-sided array lookup.
MrJoeyJoeJoey
#180968119Sunday, January 03, 2016 4:41 AM GMT

cnt, are you in college or do you like program computers for a living?
cntkillme
#180968144Sunday, January 03, 2016 4:41 AM GMT

I'm in high school and I have no life
MrJoeyJoeJoey
#180968179Sunday, January 03, 2016 4:42 AM GMT

You should make a game on here.
UnstableScript0
#180968242Sunday, January 03, 2016 4:43 AM GMT

I wish I had your life, lol. Your so smart. When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
DrHaximus
#180968275Sunday, January 03, 2016 4:43 AM GMT

lmao ^ what a nerd
MrJoeyJoeJoey
#180968332Sunday, January 03, 2016 4:44 AM GMT

^ Actully do wish I knew what he knew. Knowledge = money and money = power