How Module Scripts Work


When we run a script that uses functions from module script how does it work?

While the script is being compiled, when it sees a module.function is it goes into the module script and replaces that code and when the program runs, does it work as if that code was always there? Or does it enter the module script and run that code every time it sees a module.function while the script is running?

I asked this question because I have code that needs to run fast. It does not need to be very fast to load in the first place, but it would be good if the codes in the script run fast (Since I perform a lot of operations when the cannonball hits something, I want to optimize it as much as I can)

    cball.Anchored = true
	cball.CanCollide = false
	cball.Transparency = 1
	cball.ParticleEmitter2B.Enabled = true
	cball.ParticleEmitterKahve.Enabled = true
	local explosion2 = Instance.new("Explosion")
	explosion2.Parent = script.Parent
	explosion2.Position = cball.Position
	explosion2.BlastRadius = 20
	explosion2.ExplosionType = Enum.ExplosionType.NoCraters
	task.wait(0.5) --waiting for particle emitters to finish
	cball:Destroy()

if u want to test it i can share the game

and sorry for my terrible grammar

1 Like

ModuleScripts run in their own environment like any other script does, which would make its execution more akin to your second image. That being said, you will not see any appreciable performance difference between the same code executed from a required ModuleScript versus that code being run directly in the original Script.

If you’re calling the function an insanely-large number of times per second (like, tens of thousands of times a second), then you can start to see benefits from localizing the function call rather than looking it up from the module each time, like this:

local Module = require(ModuleScript)
local Function = Module.Function

Function()

Again, this is only beneficial in cases where the function is being executed many, many times per second. I’ve been guilty of falling into the trap of localizing every module function call in the past, and all it really does is make code harder to work with. In virtually all cases, the execution time of the function being called will dwarf whatever time the code spends looking up the function in the module’s table.

The primary use case for ModuleScripts is to make your code more, well, modular and portable. The flexibility this offers and the improvements to organization it allows should not be undersold, and it’s certainly not worth giving up for any very small gains you might be able to eek out.

2 Likes


You can analogously associate it with things you can place into a slot, before you boot up the hardware. When code is compiled, it simply reads the module script from the line it is being required.

(i also dont quite understand module scripts)
So when we call the module script eith require, does it simply like when we call it it runs completly idk

1 Like

No, it’s like pasting the other script in the exact line it is being required. You can execute its functionalities from the outside under the assumption the module script has declared some functions for its exports.

They are two separate contexts, knowing that even if you have one variable named the same in both scripts, they wouldn’t be the exact same thing.

When a script uses a function from a ModuleScript in Roblox, the ModuleScript is executed the first time it is required, and its return value (usually a table of functions or data) is cached. This means the code inside the ModuleScript runs only once. After that, whenever a module.function is called, it accesses the function from the cached return value without re-executing the ModuleScript. The script does not replace the function code at compile time but instead refers to the cached module every time the function is invoked during runtime.

1 Like

If I require the same module script in 5 different scripts, will the module script be cached only once? Or will it be cached for each script?

Edit: That was a bit glossed over🤣… Let’s be exact here.

The ModuleScript is cached once, only the first time it is required, and all module.functions within the ModuleScript are stored as part of that cached table. Each time you call a module.function, it executes that function stored in the cached ModuleScript table.

Note: This is a perfect example of how you use a ModuleScript.

2 Likes

This is great! It not only makes organizing codes easier, but also reduces memory usage.

What I meant by that was you are calling this from many scripts for use. I see people forcing a ModuleScript when they are only calling it from the one script. That should be a function and not a ModuleScript. They are really sweet, but, they do have a case usage that should be followed.

1 Like

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