How are Module Scripts Loaded When require()'ed?

Hopefully a quick question. I’m wondering what happens when you require() a module script. Is the code from the module replicated, and then run inside the script calling the module? Does the code run inside the module being required?

I’m leaning towards the latter option since you can Enable and Disable player controls from outside the Roblox Control Script, but I’d like to know for sure how the code is ran.

The code runs inside of the module. The module runs when it is required for the first time, and then never runs again.

1 Like

So if I were to make a module to handle all UserInput, I would have no issues with separate scripts adding and removing events in that module? Example:

Script 1:

inputHandler:AddEvent("Q", Priority.Low, func123)

Script 2:

inputHandler:(AddEvent("Q", Priority.High, funcXYZ)

In this scenario, I would only have the high priority function being called correct?

I’m not sure I understand, but my answer is that multiple scripts can modify and overwrite things inside of a table in a ModuleScript, and the scripts can cause each other problems by doing so. I’m not sure if that’s what you are asking of course.

My other answer is (assuming I understand exactly what that code does) that you will have no problems, two separate functions will be assigned to Q, with two separate priorities. The way you implement AddEvent determines what happens, if AddEvent overwrites the other of course then you might have problems. (I.E. assigning a variable ‘Q’ in a table and then changing it when AddEvent is called again)

4 Likes