Global variables or instance attributes?

I want to share data between my functions, should I create a global variable or use instance attributes on my script?

2 Likes
_G.dat = {}

or

shared.dat = {}

or use a modulescript

1 Like

script scoped variables, not _G.

I am trying to save a property like “is the player touching a block?” and I will use it in only 1 script, I don’t understand how a ModuleScript would help.

I would honestly use attributes, they are more comfortable than global variables or values instances.

and much easier than metatables

1 Like

Global variables are code smell.

Explanation repeated 99847e48 times on this forum on why global variables are bad
  1. Global variables are indexed on the script global table. Local variables are indexed by offset in memory. Thus local variables are faster.
  2. They can be accessed from other parts of the script even if declared in an obscure loop-if chain.

You can literally define a local variable at the header/top of the script. It will then be accessible from anywhere in your script. No global variables, no attributes, no ValueBases, just a simple mere local variable. ( ^ _ ^ )

1 Like

Yes, I agree.

How could I forget that?

1 Like