How to run the same module in multiple environments

I have a ModuleScript where I put data on a table in it. The thing is that some times I require the module from other scripts in the same environment or different ones (like the command bar) but the content in the table changes to default. I know I can easily fix this with _G or saving values in ServerStorage or something, but I’m looking for best efficiency and performance, and I heard using _G is bad most times. Any ideas?

Use Shared, this seems to be useful for you.

You can use something as simple as this.

local module = {}

module.Table = {}

return module

Note that local scripts and server scripts see and interact with the module differently. The console in playtesting can be both client and server sided so be careful for that. The console also runs in a different environment than other scripts, which can cause this issue.

shared and _G are pointless to use, as module scripts are saved to memory. Which means each script that requires that module script will see the same data. (Server and Client will be different obviously)
shared and _G are slow performance wise as modules are a lot faster, and you don’t have to wait for the _G or shared values to be made on other scripts.

The thing is that no matter how hard I try for it to not happen, sometimes when I require the module in the server it’s like if a whole new environment was created and I don’t even know why. Also I usually have to require the module from different environments like the command bar in Roblox studio, the console in the game, even through a plugin and it messes up because it’s a Data module and it reads “blank” data

Anyone knows any solution? Or maybe another post where I can get some clue or smt, my module is broken and because I made it a package it’s breaking all my games