Is it good to use lots of variables?


  • Is it good to use lots of variables while coding outside of loops
  • Is it good to use lots of variables inside of loops
  • Is it good to use lots of variables in general

I get that it makes lots of lua heap. But is that an issue?


From what I understand if you are only going to reference an item once then making a variable for it just takes up more room.
If you are referencing it more than once it’s a good idea to use a variable.
It’s great for calculations, cleans up code, and if you need to make a change to something (like setting a timer used in different places) then you only have to change it once in the code.

1 Like

Do you need it more than once?

  • if so, yes. use a variable.
  • if no, then don’t, it will clutter up the memory.

What about if it’s in a loop that happens every frame, for a calculation that is used 3+ times

1 Like

You should totally send the actual code instead of giving vague descriptions of what you want


Then how would a long reference to an item each time be better than variable = (whatever) if you are repeating it that many times?

I will assume this is a general scripting question and will answer it as such.

Luau has a local variable limit of 200 per scope, if you find yourself being near this limit, it might be time to further refactor your code into modules or functions.

I have never hit the 200 variable limit (except when going out of my way to), there’s also a register limit of 255, but this only becomes a problem if you’re doing some very questionable code

1 Like

If it’s something in a loop, make a variable outside of the loop for it. Ive seen good preformance increases doing this.

Thank you all for the help! Also I did just want to get peoples ideas on it

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.