Understanding ModuleScripts?

I’ve started working more with ModuleScripts and find them fascinating! I just have a query in terms of actually using the require function.

First of all, is there a limit on how many times a ModuleScript can be “required” from different sources before it begins to affect performance?

Secondly - Understandably when you Require a modulescript it “Resets” dependant on what variables are outside of the functions at the time.

So example:-

local isOpen = false
Door.Control = function()
isOpen = true
end

Each time it’s required, it’ll be set to False, regardless of whether or not its already true.
Any work arounds or suggestions?

Nope

A ModuleScript is a type of Lua source container that runs once

More likely the issue is that this variable can be shared by many scripts that require the module. To solve this use object oriented programming to create a property table that belongs only to one door object.

a MouleScript is executed only once. This means that, the result of its execution, what we put in the return, is stored in memory, like any other object. The ModuleScript will be executed the first time it is required, the rest will simply return the result of that first execution. So, there are no limits to access that result. The performance will depend on what you are doing and not on the ModuleScripts themselves.

Regarding your second question, basically all the variables that are still being referenced after the ModuleScript is executed are still in memory with their value intact no matter how many times the ModuleScript is called. Remember that the ModuleScript is only executed once.

I hope this has been helpful.

1 Like

Actually no, module scripts are loaded once and then shared across every thing that requires it.

Btw, this is only shared across client-client and server-server, not server-client or client-server