Creating with Module Scripts

20 min

Creating with Module Scripts

Player in front of treasure chest and lockpick

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

  1. Download the starter project.
  1. 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.

  1. In ServerStorage, create a new ModuleScript and rename it TreasureManager.
  1. In TreasureManager, rename the default module table by replacing module with TreasureManager 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

  1. This module script will use a combination of module and local functions, type two comments to help you keep them separated.
  1. Under the Module Functions comment, add a new module function to TreasureManager named getLockpick().
Use two parameters:
  • lockpickPart - the part to destroy.
  • whichCharacter - the player that touched the lockpick part.
  1. In getLockpick(), destroy lockpickPart.

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.

  1. Open the Lockpick script in Workspace > Lockpicks > LockpickScript.
  2. In LockpickScript, store the module script in a variable named treasureManager and set it equal to:

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.

  1. There’s already a function named partTouched() to check for a player touching the part. Inside partTouched():
  • Call the getLockpick() module function to destroy the lockpick.
  • Pass in lockpickPart and whichCharacter.
  1. Run the project and check that touching a lockpick destroys it.

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 = {} and return 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.

  1. In ServerStorage, open the TreasureManager script.
  2. 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.
  1. Copy and paste these two local functions into the Local Functions section.
  • getPlayerLockpicks() returns the value of the player's Lockpicks leaderstat.
  • getPlayerTreasure() returns the value of the player's Treasure leaderstat.
  1. To add to the player’s lockpicks, in the getLockpick() module function:
  • Create a local variable to call getPlayerLockpicks(whichCharacter)
  • Add the value of lockpickDrop to playerLockpicks.
  1. Run the project. Check that touching a lockpick destroys it and adds 1 to the player’s lockpicks in the leaderboard.
  • Check that all local functions are above any module functions. A module function won’t be able to use a local function that’s below it.

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

  1. 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.
  1. 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 named canOpenChest() with the parameter whichCharacter.
  1. Copy and paste the code below into canOpenChest() to return true if the player has enough lockpicks, and false if they don’t.

Give Players Treasure

If a player can open a chest, create a function in TreasureManager that awards them treasure.

  1. Add a new module function to TreasureManager named openChest().
Pass in two arguments:
  • chestPart - the chest part to destroy.
  • whichCharacter - the player to give treasure.
  1. 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, like chestReward, 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.

  1. In Workspace > Chests > open ChestScript.
  2. Create a new variable named treasureManager and require the TreasureManager module script in ServerStorage.
  1. In partTouched(), under the if humanoid statement, create a new variable named canOpen and set it equal to:
  1. 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, and whichCharacter, the player to award treasure.
  1. 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.
  • 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 Project Sample

Project File

Download the finished project here.

Finished Scripts