Is this considered bad practice? (200 variable limit workaround)

Is using a table to work around the 200 variable limit, considered bad practice for future code?


local var = {}
var.Player = game.Player.LocalPlayer
var.Mouse = var.Player:GetMouse()

Then just keep assigning new variables to the table var as you make progress to your code, would this then be considered bad practice? Or is there a better way for exactly this?


This is identical to using global variables. Doing this is redundant when you can just do:

Player = game.Player.LocalPlayer
Mouse = Player:GetMouse()

Global variables are stored in a regular table known as an environment.

1 Like

Going over the 200 variable limit is bad practice. This usually means you have a god module/script (something that has too many tasks, rather than one specific one) that you should really be splitting up into multiple smaller modules with specific purposes, or to encapsulate (part of) that data into objects.


So what you’re saying is, instead of having a lot of variables in one module/script, I should split the module/script into smaller ones?

1 Like

You should always strive to write code that is modular, this is much easier for yourself to keep track of and maintain.

It’s hard to give specific advice without knowing your specific example. If you share some code on what kind of variables you currently have I could give more specific advice.


It’s not what I currently have or try to do right now, it’s more of some old problems I’d like to not face again in the future. In summer 2018, I wrote a framework which hit the limit of 200 variables which was all in one script. In that moment I understood how important modules and multiple scripts are, which got me to try and save the framework by splitting it into modules, but it got too difficult and I decided to restart the project and remake the framework (which I probably will in late February or early March 2019).

I’m simply wondering about how I can avoid this happening to the new framework. I will try and use multiple modules as I’ve seen top developers do. The framework I made in summer 2018 had unnecessary variables that I could’ve stored in a configuration module or similar, where one variable stored one things like one motor6D, or one rig bodypart, or a configuration value etc.

1 Like

If you’re determined to have it in one script, note any instances where variables are created for a single block of code and then never used again. You can stick those blocks of code into a function, or a do block.

local variableOne = 'something required in multiple blocks'

    local variableTwo = variableOne:sub( 1, 23 ) .. 'a single block'
    print( variableTwo )
    local variableThree = variableTwo:len()
    print( variableThree )

print( variableTwo ) -- doesn't exist here

This still uses up local slots; when they go out of scope the slot gets freed for temp use again but not for assigning another local variable to.

Question: If most of my variables are meant to be used in various functions/loops,
would this not count towards the limit of 200 variables if I defined them as someVariable = "someString" instead of defining them as local someVariable = "someString"?

1 Like

Each individual function has its own 200 local variable limit along with a 60 out of scope (upvalue) local variable limit. There is no global variable limit because the global environment is just a masked table, while local variables are held in a special, easy to get Lua stack.


If I understood your answer correctly, it’s correct that by defining it without the local, not count towards the limit?

1 Like

Yes, but this may make the flow of your code hard to follow in terms of where variables are used. You should also only do this for variables that aren’t used from different threads – so no using it in connections or callbacks unless it must be global. There’s also a small performance overhead but only matters where your code must be absolutely fast.

No, it would not count towards the local variable limit. The amount of global variables has no (theoretical) limit.

To just finish off the thread and solve the OP question; What’s the best way to work around the 200 local variable limit?

1 Like
  1. Use global variables. There is no limit to the amount of global variables you can use, or…
  2. Rethink the design of your code. Try to modularise it into smaller, related units of code.
1 Like

If you’re hitting 200 variables in a single ModuleScript, there is most definitely a problem in the way you are designing your codebase. You should not have a case in which this happens. Split your data table into relevant sub-data containers, don’t keep it in a super-module.

I hate global variables and would never encourage using them. I’d rather have my variables local to the current scope.


I assume he’s hitting the limit in the global scope, so this wouldn’t necessarily be an issue.

This was in a localscript actually.

1 Like

Doesn’t majorly change my response. Branch your variables and constants out to ModuleScripts. 200 or more in one script is fairly overkill. I don’t even like the thought of using 2/5ths that amount.