I have a ModuleScript which has been “required” by two different Scripts, both parented under ServerScriptService.
I added a print("Hello world") within said modulescript and it prints twice, for each time it was “required” - aren’t they supposed to run ONCE per environment?
This is troubling because I’m using the modulescript to share tables, and using _G seems like a terrible idea.
Everytime a module script is required, it runs again.
Something else to note is that when an asset id is required, it is cached. So the actual module script isn’t created again when requried twice. This is unrelated but it’s worth saying.
After reading documentation, It appears I was wrong about one part, which was that it can only run once per Environment, but return the exact same thing when using require()
So basically, the first time you require a ModuleScript in an Environment, it will run something, but if you do it again on another script, it wont run and just returns a table, however if you run it on a difference environment, it will run the code, but do the exact same thing, even if that does happens where it runs again, it shouldn’t cause issues
It depends how the script has been set up, you can use a module script to act as a singular storage however you’d also need to create definitions to support this (and not return the actual module, but instead the operator modules).
If I am correct:
my_shared = {} -- This would be a shared container
my_module = {} -- This would be a module container which is returned
-- You can now use my_module to effect my_shared which should be the same as _G
return my_module
I believe this is the case, could be wrong though as I haven’t been on studio for a while. Just saw this and thought I’d input. This doesn’t cross the server-client boundary though, so their can be two of the same module scripts on each side.