Creating with Module Scripts
Creating with Module Scripts
To apply your knowledge of module scripts, create a module script that allows players to pick up lockpicks and use them to open treasure chests.
Project Setup
This project includes a starter map with leaderboard and scripted pickup objects for the lockpicks and treasure chests.
Load the Starter Project
- Download the starter project.
- In Roblox Studio, open the downloaded file: Intro to Module Scripts - Starter Project.rbxl.

Create a Module Script
So players can get treasure from chests, create a module script named TreasureManager. Using a module script will connect the pickups and leaderboards together.
- In ServerStorage, create a new ModuleScript and rename it TreasureManager.

- In TreasureManager, rename the default module table by replacing
module
withTreasureManager
in both places.
Using Functions in Module Scripts
To test how functions work in module scripts, create a new function named getLockpick()
. When the getLockpick()
function is called from another script, it’ll receive a lockpick part to destroy and add 1 to the number of lockpicks in the player’s inventory.
Create a Module Function for Lockpicks
- This module script will use a combination of module and local functions, type two comments to help you keep them separated.
- Under the Module Functions comment, add a new module function to
TreasureManager
namedgetLockpick()
.
lockpickPart
- the part to destroy.whichCharacter
- the player that touched the lockpick part.
- In
getLockpick()
, destroylockpickPart
.
Use the Module Function
Now, the module function getLockpick()
can be used in other scripts. To test that function, you’ll open a premade script and call it.
- Open the Lockpick script in Workspace > Lockpicks > LockpickScript.
- In LockpickScript, store the module script in a variable named
treasureManager
and set it equal to:
require(ServerStorage:WaitForChild("TreasureManager"))
Why WaitForChild()? »
Prevent errors by using WaitForChild()
instead of the dot operator to make the script wait until TreasureManager
has loaded before moving on. For scripts in ServerScriptService or ServerStorage, it’s safe to use the dot operator instead, like in ServerStorage.TreasureManager
.
- There’s already a function named
partTouched()
to check for a player touching the part. InsidepartTouched()
:
- Call the
getLockpick()
module function to destroy the lockpick. - Pass in
lockpickPart
andwhichCharacter
.
- Run the project and check that touching a lockpick destroys it.
Troubleshooting Tips »
Issue: Get an error message including: "Infinite yield possible"
.
- Check the spelling of your module script in a script. If a module script, like TreasureManager, is spelled differently, there will be an error.
Issue: Get an error message including: "attempt to index global"
.
- Check the line that includes the require for the module script in LockpickScript. If the module does not include require, it can’t use functions and variables from that module script.
Issue: Script doesn’t run or can’t pick up lockpicks.
- In the module script, make sure that all the code is between
local TreasureManager = {}
andreturn TreasureManager
. Any code outside these lines can’t be seen by other scripts. - Check that there are two parthenessis at the end of the line with require, like in
WaitForChild("TreasureManager"))
.
Create a Local Function
To modify a player’s lockpick and treasure leaderboard stats, use local functions in the module script. The functions that change a player’s variables are local functions since they’re only used within this module script.
- In ServerStorage, open the TreasureManager script.
- Create local variables to do the following:
- Get the Players service so the script can work with player's leaderboard stats.
- Store the number of lockpicks the player receives after touching LockpickPart.
- Copy and paste these two local functions into the Local Functions section.
getPlayerLockpicks()
returns the value of the player'sLockpicks
leaderstat.getPlayerTreasure()
returns the value of the player'sTreasure
leaderstat.
- To add to the player’s lockpicks, in the
getLockpick()
module function:
- Create a
local
variable to callgetPlayerLockpicks(whichCharacter)
- Add the value of
lockpickDrop
toplayerLockpicks
.
- Run the project. Check that touching a lockpick destroys it and adds 1 to the player’s lockpicks in the leaderboard.
Troubleshooting Tips »
- Check that all
local
functions are above any module functions. A module function won’t be able to use alocal
function that’s below it.
Current TreasureManager Script »
Getting Information From Module Scripts
The TreasureManager module script will be used when players touch a treasure chest to check if they have at least one lockpick before opening it and giving them gold.
Check if Chests can Be Opened
- First in ServerStorage > TreasureManager script, set up variables for how many lockpicks it costs to open a chest, and how much gold each chest contains.
- To create a function that checks if a player can open a chest, in the Module Functions section, add a new function to the
TreasureManager
table namedcanOpenChest()
with the parameterwhichCharacter
.
- Copy and paste the code below into
canOpenChest()
to returntrue
if the player has enough lockpicks, andfalse
if they don’t.
Give Players Treasure
If a player can open a chest, create a function in TreasureManager that awards them treasure.
- Add a new module function to
TreasureManager
namedopenChest()
.
chestPart
- the chest part to destroy.whichCharacter
- the player to give treasure.
- To subtract a player’s lockpicks and award them treasure, copy and paste the code below in
openChest()
. This code uses the variables created previously, likechestReward
, the amount of treasure given per chest.
Call the Chest Functions
Now that the two module functions, canOpenChest()
and openChest()
, have been created, they can be called by the Chest parts whenever a player touches them using the premade partTouched()
function.
- In Workspace > Chests > open ChestScript.
- Create a new variable named
treasureManager
and require the TreasureManager module script in ServerStorage.
- In
partTouched()
, under the if humanoid statement, create a new variable namedcanOpen
and set it equal to:
treasureManager.canOpenChest(whichCharacter)
- Next, create an if statement to check if
canOpen
is true.
- If so, call the TreasureManager's
openChest()
function. - Then, pass in two parameters:
chestPart
, the chest to destroy, andwhichCharacter
, the player to award treasure.
- Run the project. Check that:
- If you have at least 1 lockpick, touching a chest will destroys it and awards treasure.
- If you have 0 lockpicks, you can't open a treasure chest.
Troubleshooting Tips »
- In ChestScript, make sure that functions called from the module script like
canOpenChest()
are spelled exactly how they’re found in the TreasureManager script. Any difference will cause an error. - Check that copy and pasted functions, like
treasureManager.openChest()
, are exactly as shown in the lesson. Any differences can cause subtle errors in the script.
Finished TreasureManager Script »
Finished ChestScript »
Finished LockpickScript »
Previous Intro to Module Scripts