ModuleScript runs more than once in a single environment

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.

Am I missing something?

This is normal, ModuleScripts can only be called once per script.

…but the print statement is printing more than once, suggesting that it isn’t the case for me right now…

Yes, because when you require a module, it fires the code within the ModuleScript. While returning its contents

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.

1 Like

Okay, that’s cleared some things up. Thanks. How would I share tables between scripts though?

You could save the return of the module in _G, or just keep it at the top of an important script. It depends on what organization you’re looking for.

1 Like

I’d rather avoid using _G if possible.

I posted this question in the first place because the behaviour I was experiencing contradicts with what is happening in this post.

keep in mind he said The ModuleScript is only initiated once on each client / server which is referring to a Script

1 Like

Aha, thanks. That makes sense.

Begrudgingly it seems like this is my only solution. Thanks for everyone involved.

(I’m struggling to mark this as the solution due to some weird 500 error)

Anyway so (maybe) long post,

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.

ooh? I’ll definitely give this a try.

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