Module scripts execute once on first require?

Hello,

I have been working a bit with module scripts and I have noticed that the “loose code” within a module script is ran only on the first require on server and client. Does it cache permanently and does this happen every time?

I have read somewhere that whenever requiring an executed module script it just returns the module without running any of the code. Is this what happens?

A ModuleScript is a type of Lua source container that runs once and must return exactly one value. This value is then returned by a call to require given the ModuleScript as the only argument. ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require .

from:
https://developer.roblox.com/en-us/api-reference/class/ModuleScript

1 Like

Thank you for confirming :slight_smile:
I should have read the api carefully.

Note that soon this may no longer be the case after Parallel Lua is live. The module may run multiple times depending on how many VMs need it. This will be a massive pain for me when implementing multithreading, so if you’re interested in using this new feature you should avoid relying on modules running once.