ModuleScripts

ModuleScript objects are scripts that export a value to import in other scripts. You can implement them to reuse code among multiple LocalScripts and/or Scripts.

Creating ModuleScripts

To create ModuleScript objects using the Explorer window in Studio:

  1. Hover over the parent into which you want to insert a script. In most situations, you should place ModuleScripts in ServerScriptService when using them with Scripts, or in ReplicatedStorage when using them with both LocalScripts and Scripts.
  2. Click the button that appears to the right of the container to open the Insert Object menu.
  3. Select ModuleScript.
  4. Rename the ModuleScript to reflect its purpose using PascalCase.

Structure of ModuleScripts

Each ModuleScript starts with the following code:


local module = {}
return module
  • local module = {} creates an empty table.
  • return module returns the table and its members to any script that imports the ModuleScript.

ModuleScript objects return one value that can be any data type except for nil. You can execute arbitrary code in a ModuleScript, but you only need to return what you need in other scripts.

For more information on adding variables to a table to return in a ModuleScript, see Tables.

Requiring ModuleScripts

A ModuleScript runs only when another script imports it using the require() function. If a ModuleScript requires another ModuleScript, a Script or LocalScript must require the first ModuleScript in the chain for any of them to run.

To access a ModuleScript from another script using the require() function:


-- Script in ServerScriptService
local ReplicatedStorage = game:GetService("ReplicatedStorage")
-- Get the return value for the ModuleScript named "PickupManager"
local PickupManager = require(ReplicatedStorage:WaitForChild("PickupManager"))

When you call require() on a ModuleScript, it runs once and returns a single item as a reference. Calling require() again returns the exact same reference, meaning that if you modify a returned table or Instance, subsequent require() calls return that modified reference. The module itself doesn't run multiple times.

If you require() a ModuleScript from both sides of the client-server-boundary, such as in a Script and a LocalScript, then the ModuleScript returns a unique reference for each side.

Example ModuleScript

To return a getPickupBonus function in the PickupManager table:


-- ModuleScript in ReplicatedStorage
local PickupManager = {}
local defaultMultiplier = 1.25
local rarityMultipliers = {
common = 10,
uncommon = 20,
rare = 50,
legendary = 100
}
-- Add the getPickupBonus function to the PickupManager module table
function PickupManager.getPickupBonus(rarity)
local bonus = rarityMultipliers[rarity] * defaultMultiplier
return bonus
end
return PickupManager

To call the PickupManager.getPickupBonus function:


-- Script in ReplicatedStorage
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