What is the best way to store a table for later use (in the same session)

Say I have a table. This table stores some information that will probably be changed frequently by the same script. What is the best way to store said table without risking losing it at any point? Should I use _G? I know a lot of people seem to hate _G so I’m unsure

If it is only used in one script just store it as a variable. If it’s more than one script store it in a module. Using _G is the same as storing it in a module that is automatically required in every script, so don’t do that unless it really is used that way. Additionally, you have no control over the order of operations done to _G, so there’s almost never a good reason to do it.

1 Like

To add onto azqjanna’s answer, one of the problems with _G is you can’t verify any of the data with it.

Say you have an initialization script that sets up a value called _G.PlayerData, and then several other scripts that are relying on it. If everything runs in order, you’re fine, and the game works.

But then you add a new feature later on, and in some of the code for that, you accidentally reassign _G.PlayerData to something else. When that code runs, everything else relying on it will break, and you won’t really be able to pinpoint where the cause is (you could look into the code that you’ve changed, but it could be a really long debugging process).

If you leave it with ModuleScripts, you know where the data is coming from. One script reassigning it would cause issues in that script, but other scripts would still run fine. And from there it makes it a lot less code for you to look through - you’d just need that one script that isn’t working.