Upcoming change to (correctly) limit the local count to 200

This is highlighted in release notes for 428 but it’s worth a separate announcement.

TL;DR: Make sure your functions with hundreds of local variables still work in Studio, before they break in live games!

A bug report ("Out of registers when trying to allocate 4 registers: exceeded limit 255" What does it mean?) highlighted a small issue in Luau compiler that existed since we launched Luau last year - instead of enforcing the limit of 200 locals, the compiler enforced the limit of 255 locals. Which means that you could use 210 locals in Luau VM.

This is problematic because 255 also happens to be the limit for temporary registers. If you use, say, 250 local variables, the remaining expressions in your function only get up to 5 registers. At this point even simple expressions may run out of registers when compiling your code.

This doesn’t seem like a big problem, but it actually makes your code very fragile - not only can small changes in your own code lead to the code no longer compiling (as was witnessed by the bug report), but also small changes in our compiler can lead to your code not compiling anymore!

Because of this, we fixed this problem and now the limit of 200 locals is enforced correctly. We also helpfully point the compiler error when running out of any limited quantity (200 locals, 200 upvalues, 255 registers, 5 packs of chicken legs) to the correct location in source.

Just to be clear - these limits are per function. As long as each individual function conforms to these limits, it doesn’t matter how many functions you have in your script (apart from your own comfort when working with that code).

However, it’s possible that some of you have accidentally written code in the last few months that uses more than 200 locals. This code will become invalid as a result of this change and will refuse to compile.

Code like this was not valid in old Lua VM, but it was valid in Luau VM up until now.

This change is currently active in Studio, but not in live games. So if any of you have scripts with lots and lots and lots of locals, please check that Studio can still run them correctly and adjust the scripts if necessary to use fewer locals.

The change will go in effect in live games next week.

133 Likes

Dang, I guess I have to move to Unity to have 6 packs of chicken legs :pensive:

In all seriousness, this is a good move. Clarity is great, even if it means we get a few less local variables available.

44 Likes

I was under the thoughts that chicken legs were an infinite quantity…

21 Likes

Don’t tell Shedletsky that they aren’t. He would be extremely infuriated.

47 Likes

I’m just wondering why someone would use 200+ local variables…
Also I only came to roblox for the chicken legs, like jeez.

10 Likes

In my opinion it is impossible to get 200 local variables (and even less impossible) to get over 200. Though I guess some people have done it. Thank you for clarifying and the error according to the OP linked has been fixed to make more sense.

201 local variables when
7 Likes

Thanks for the warning! I actually have a plugin that generates code that used 255 as the limit, so prior warning is appreciated.

7 Likes

Does the 200 locals mean, 200 locals in the script scope, or ALL of the locals in a script (Loops, cases etc)?

1 Like

If you have over 200 local variables in the scope then you seriously have issues in your programming and need to restructure the whole thing. (not talking about you specifically since I’m agreeing with you).

local variables are supposed to have a short life, we use local variables to avoid cluttering the global environment with unnecessary names and accessing them is obviously faster than accessing global ones.

If you need more than 200 locals then use do blocks/ create new scopes, locals that you do not need in the main chunk can be put in a new scope.

10 Likes

So just to clarify is this just per function or also for the whole script excluding functions?

2 Likes

The limit is per function, just like the 30 character limit is per post.

16 Likes

I do agree that you need to restructure your code, however if you “somehow can’t” you can always try to store things as values inside of dictionaries.

For example,

local variables = {
   ["Var1"] = true,
   ["Var2"] = workspace.Baseplate
}
7 Likes

@ everyone asking how do you get over 200 locals, keep in mind people still obfuscate their Lua code thinking it actually does something. As a result, many times these obfuscated scripts have a ton of local variables to help cover something like game:GetService("HttpService").

4 Likes

Do blocks don’t enable you to surpass the 200 local limit.

4 Likes

That’s not what he was trying to say, from my understanding. He’s saying if you define some of your 200 that you don’t need for the whole function in a do block, and you only use them in there, it clears up space in the main function for more locals. For example:

local x, y = 1, 2
do
	local z = x * y
	x = z ^ y
end
-- you now have space for 198 locals since you don't need z anymore
5 Likes

I thought something was weird, I was testing with a plugin the was broken in the old lua, but worked in the new luau. Turns out I wasn’t going crazy, so glad this is being fixed.

Seriously, though, if you use 200 local variables in one scope, use module scripts please. (Or remove your insane way of programming)

2 Likes

I guess I misunderstood what he was saying because of the way he phrased it, but your example still doesn’t make sense to me since that do-block still takes up a local, so you’d only have 197 locals left because you defined “z” within the block.

1 Like

local variables are restricted to their own scope.

local hello = true -- this is a local to the chunk

local function xd()
   local hello = true -- this is a local to the function not to the chunk
end

do -- create a new scope
   local hello = true -- this is a local to the do block
end

When you create a variable in a different scope it doesn’t add up to the “main” or “chunk” so to answer your question, when you create a local variable in a different scope it’s only local to that scope and is counted for that scope only.

1 Like

I have a code with 3500 lines and yes, I had more that 200 local variables, xd

3 Likes

Wait I think he already saw…

1 Like