Module Scripts are a powerful tool centered around a singular idea: allow code to be accessed from other scripts. If used properly, they can save hours of time and lines of code. Many modern games are built with almost 100% module scripts. If you know how to use module scripts, you can find yourself writing cleaner code that is easier to manage as the scale of your game increases.
Prefer video formats? Watch this!
How to create a Module Script
To create a module script, select the location you want to place it in, click the plus or insert object, and simply select module script.
Note: Module scripts adhere to the client/server boundary rules. A module script created in ServerScriptService will NOT be available on the client
Once your module script is created, you should see this:
local module = {}
return module
This may look different than the print("Hello World!")
you are used to seeing upon creating a script. This is because module scripts can only function so long as a fundamental principle is followed: they return something.
Whether it be a table, a boolean, or a singular function, whatever the module returns can be accessed by other scripts.
99% of the time you use a module script, the goal is for it to return a table. This table can store data and functions, just as they would in a normal script. Anything put in the module table is returned, and thus accessible in whatever script retrieves this module.
The proper way a module is typically set up is like this:
- Variables:
module.textToPrint = "Hello world!"
- Functions:
function module:Print()
print("Hello world!")
end
Both of these add the data to the table, meaning they are returned. Variables and functions within a module function exactly the same as they would in a normal script, but they must be indexed with the table’s name. There are some more facets to this with the introduction of the self
keyword, but that’s for another time.
How to implement a Module Script
Implementing a module script is very simple. It is the same as referencing an object, but with a method wrapped around it.
What is the difference between these two lines of code?
local module = game.ServerScriptService.ModuleScript
local moduleScript = require(game.ServerScriptService.ModuleScript)
The first line is a reference to the actual module script object. This allows you to index common properties such as the name
, parent
, and more. This will NOT allow you to access the contents of the script. In order to do that, you must do as the second line did, using the require
method. This will take a module and retrieve the value that is returned by it. That is why it is important that anything you want accessible by another script is added to the module’s table and returned. You cannot put code under the return statement in your module.
Once you have required the module, you can fire functions and access variables with ease:
moduleScript:Print()
print(moduleScript.textToPrint)
As you can see, modules can allow you to stick to a key programming principle: Don’t repeat yourself.
Any function you need to use multiple times, even with different inputs, can be placed in a module with parameters.
I hope you guys found this article useful, let me know if you have questions or if there are any issues/inconsistencies within this article. Thank you!