Roblox Require Module Script

Today, Roblox players do not need to copy the code to multiple scripts as it wastes a lot of time. Alternatively, they can utilize the ‘ModeluScript’ to organize and reuse the code as the better way in copying the code.

So, what is ModuleScript? In Roblox, ModuleScript can be defined as a unique type of script which will return a single value, commonly a table or function which is useful to multiple scripts. However, ModuleScript does not run the code by itself, but it will need the ‘Require’ function.

How Does the ‘Require’ Function Work in ModuleScript?

ModuleScripts in Roblox will not run its code by itself, instead, it will need another script to access it by using the built-in ‘require’ () function. ‘Require’ function will accept the Module Script as its only argument.  Well, the location of the module script is in the Explorer.

For example, if a ModuleScript is located within ReplicatedStorage, you’re able to access it from another script as follows:

    1. local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
    2.     
    3. — Get value returned by ModuleScript
    4. local PickupManager = require(ReplicatedStorage:WaitForChild(“PickupManager”))

‘Require’ in ModuleScript will run once. It will then return a single value. To return the exact same table, you can call ‘require’ again in the script. Need to know, the module itself will never run multiple times.

In other words, it will only apply when ‘require’ is called from either a server-side Script or client-side LocalScript. If you ‘require’ the module from both sides, a unique table will be then returned for each side.

Well, to use a module in a separate script, you can set a variable equal to ‘require(moduleScript). Here’s for the example:

1.local MoneyManager = require(ServerStorage.ModuleScript)

Now, the variable myModule definitely contains the module table which is created in that module script. If you want to use the functions and variables from that table, you need to type the variable name, followed by a dot and the exact name of what to use in that module script, like myModule.myFunction(). When the script runs and then reaches that line, it will access that specific function or variable which is stored in the module table.

How to Use the Functions of Calling Modules?

Module should return something, that’s the return value of the required function. In this case, all codes are run in the module when called and the return type is returned. It’s commonly a table of properties, functions and events or also a function which does something to the game. Somehow, the return type will be anything, including nil.

After the module is loaded, you definitely can call any of its functions with a similar dot notation format. Here’s it!

ModuleScript

1.

2.

3.

4.

5.

6.

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17.

local PickupManager = {}

 

local defaultMultiplier = 1.25

local rarityMultipliers = {

common = 10,

uncommon = 20,

rare = 50,

legendary = 100

}

 

— Add a function to the module table

function PickupManager.getPickupBonus(rarity)

local bonus = rarityMultipliers[rarity] * defaultMultiplier

return bonus

end

 

return PickupManager

Other Scripts

1.

2.

3.

4.

5.

6.

7.

8.

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)

 

— Get value returned by ModuleScript

local PickupManager = require(ReplicatedStorage:WaitForChild(“PickupManager”))

 

— Call a ModuleScript function

local bonus = PickupManager.getPickupBonus(“legendary”)

print(bonus)  –> 125

Learn About ModuleScripts

ModuleScript is a type of Lua source container which can run once and should return exactly one value. However, this value will be then returned by a call to ‘require’ given the ModuleScript as the only argument.

ModuleScripts will run once and only once per Lua environment and return the exact same value for subsequent calls to require. In other words, ModuleScripts are important objects  for following the ‘don’t-repeat-yourself (DRY) principle. Then, if you write a function, you can write it only once and use it everywhere.

In this case, having numerous copies of a function is disastrous when you have to change that behaviour. Well, you must define functions or groups of functions in ModuleScripts and have your Scripts and LocalScripts called ‘require’ on your ModuleScripts.

You need to know that the return values from ModuleScripts are independent regarding LocalScripts and Scripts and also other environments like the Command Bar. You can also use ‘require’ on a ModuleScript in a LocalScript to run the code on the client, even if a Script is already on the server.

So as in Roblox Studio, by using ‘require’ on a ModuleScript in the hierarchy with the Command bar, it will give a similar behaviour. So, you need to be careful if you’re using a ModuleScript on the client and server at the same time, or debugging it within Studio.

How to Write ModuleScripts?

ModuleScripts are usually placed in ServerScriptService when it’s used by server-side Scripts and RelicatedStorage when used by client-side LocalScripts. In this case, ModuleScripts can be used when parented to any object as long as another script has access to it as well.

Well, a ModuleScript will start out with this code, when create:

    1. local module = {}
    2.  
    3. return module

The line local module = {} will create an empty table to store the module’s contents. However, this table will be then returned (return module), so that it’s given to other scripts which access the module.

You must also rename this table and then modify the return line, according to the module’s purposes, such as PickupManager. This is also a good idea to rename the ModuleScript itself to match.

    1. local PickupManager = {}
    2.  
    3. return PickupManager
      H

How to Add a Function to ModuleScripts?

If you want to add a variable or function to the table which is returned by the module, you can use dot notation as illustrated below!

Variables

    1. local PickupManager = {}
    2.  
    3. — Add a variable to the module table
    4. PickupManager.maxPickupDuration = 10
    5.  
    6. return PickupManager

Functions

    1. ocal PickupManager = {}
    2.  
    3. — Add a function to the module table
    4. function PickupManager.getPickupBonus(rarity)
    5.  
    6. end
    7.  
    8. return PickupManager