Intro to Module Scripts | Documentation - Roblox Creator Hub (2024)

As projects become complex, it becomes important to think how scripts are organized. Good organization practices can ensure code isn't duplicated between scripts, or becomes hard to manage.

A better way to organize and reuse code is with module scripts, a unique type of script that stores a set of functions and variables designed to meet a shared purpose, like managing player money or enemies. Code within module scripts can be used by other scripts. That way, you can call the same function that gives coins from multiple different scripts whenever a player finishes a quest or finds a pickup.

By storing commonly used code in module scripts, it makes maintaining and organizing code easier since changes only need to be made to one module script, rather than updating multiple scripts.

Normal scripts should be used for standalone elements of a game, such as touching a pickup, while module scripts are useful for storing code that can be reused by multiple independent scripts, like rewarding points.

Module Script Basics

Module scripts are actually their own separate object compared to script objects. In Roblox, module scripts can be denoted with a purple icon.

Creating a Module Script

ModuleScripts are commonly placed in ServerScriptService when used by server-side scripts and ReplicatedStorage when used by client-side local scripts (such as GUI interactions).

  1. Create a ModuleScript in ServerStorage.

Intro to Module Scripts | Documentation - Roblox Creator Hub (1)

Structure of Module Scripts

When created, every module script starts out with the code below:

local Module = {}

return Module

The line local Module = {} creates a table, or container of code, where the module's shared functions and variables can be stored. This table should be renamed to the module's purpose, such as RewardManager or ParticleController. As opposed to other variables which are camel case (myVariable), module tables are recommended to use pascal case and start capitalized (MyModule).

local RewardManager = {}

return RewardManager

So other scripts can use a module's non-local functions or variables, every module ends with return MyModule. Whenever another script tries to get code from the module, return lets that script access code stored inside the module table.

Adding to Module Scripts

To add a function or variable to the module which can be used in another script, type the module table's name, followed by a dot, and the name of the function or variable, like in TestModule.myVariable. Using the dot operator is another way of adding code into a table, allowing other scripts to access that code whenever the module table is returned.

local TestModule = {}

-- Adds a variable to 'TestModule' table

TestModule.myVariable = 100

-- Adds a function to 'TestModule' table

function TestModule.doTask(player)

-- Placeholder code

end

return TestModule

Anything added to the module table should be typed between local MyModule = {} and return MyModule, or else the code may create an error.

Scope in Module Scripts

For a module function or variable to be used in an outside script, don't type local.

Typing local in front of variables and functions means they are only usable by that script. While this is a good practice for most scripts for reducing errors and troubleshooting, you cannot make module script functions and variables local.

Any code used only by that module script should still include local. For instance, the code below includes the local variable difficultyModifier, which can only be used in that module script, and the function getCoinReward(), which can be used in scripts outside the module.

local RewardManager = {}

-- Usable only in the module script

local rewardCoins = 50

-- Usable only in the module script

local difficultyModifier = {

easy = 0.5,

normal = 1,

hard = 2

}

-- Usable in other scripts

function RewardManager.getCoinReward(difficulty)

local coins = difficultyModifier[difficulty] * rewardCoins

return coins

end

return RewardManager

Using Modules In Others Scripts

By itself, a module script can't run code — it needs to be loaded in another script using the keyword require(). The function require() accepts one argument, the location of the module script in the Explorer.

To use a module, in a separate script, set a variable equal to require(moduleScript).

local MyModule = require(ServerStorage.ModuleScript)

Now, the variable MyModule contains the module table created in that module script. To use functions and variables from that table, 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 reaches that line, it'll access that specific function or variable stored in the module table.

local MyModule = require(ServerStorage.ModuleScript)

MyModule.myFunction()

RewardManager Example

ModuleScript - RewardManager

local RewardManager = {}

-- Usable only in the module script

local rewardCoins = 50

-- Usable only in the module script

local difficultyModifier = {

easy = 0.5,

normal = 1,

hard = 2

}

-- Usable in other scripts

function RewardManager.getCoinReward(difficulty)

local coins = difficultyModifier[difficulty] * rewardCoins

return coins

end

return RewardManager

Script - TreasureChestScript

local ServerStorage = game:GetService("ServerStorage")

-- Load module script

local RewardManager = require(ServerStorage.RewardManager)

--Calls function from module script

local coins = RewardManager.getCoinReward("easy")

print("Should award " .. coins .. " coins")

If you're in another script, make sure that the module script function or variable is spelled exactly the same as found in that module. To help, you can copy the exact function or variable name from the module and then just paste it in the normal script where it'll be used.

General Troubleshooting

Some of the tips here address common issues when working with module scripts. Keep in mind that module scripts can be a complicated topic with more nuance. For more details, see this more technical guide on Module Scripts.

Issue: Get an error message including: "Infinite yield possible" or "not a valid member".

  • Check the spelling of the module script where it's loaded. require() must include the exact path and spelling of the module script, which may be named differently than the module table.

Issue: Get an error message including: "attempt to index global".

  • In any scripts using a module script, make sure it's loaded using the function require(). If not, that script cannot use functions and variables from the module script.

Summary

Module scripts in Roblox are a method coders use to organize and reuse code. A module script is often stored in ServerStorage (or ReplicatedStorage for client-based interactions). From there, other scripts are able to call functions and variables stored in that module script.

For instance, one game may award points to players for collecting objects. A module script can handle code to give points. Then, scripts for different types of objects can just call the module script function. This reduces the need to reuse code between scripts, making code easier to understand and maintain.

Intro to Module Scripts | Documentation - Roblox Creator Hub (2024)

FAQs

Is Roblox scripting hard? ›

Scripting in general is hard and easy. it relies on how much effort It looks really simple but has a ton of effort and coding put into .

Can module scripts be exploited Roblox? ›

If you have a certain setting in your game thats controlled by module scripts, YES. But, If it's placed in ServerScriptService, they can't access it. If it's in Replicated Storage, they can.

What is the point of module scripts in Roblox? ›

A simple one is having the same function accessible by multiple scripts. It would be annoying to copy and paste this everywhere and if you wanted to change its behaviour, you'd need to do this every single place that you'd use this function. Modulescripts fix this.

What is _g in Roblox? ›

In the ROBLOX Lua environment, _G is a predefined global table that contains all the global variables, functions, and other values that are accessible throughout the game. You can use _G to access and manipulate these global values, as well as to define new global values.

Does Roblox use C++? ›

Engine Development: The core engine that powers Roblox games, including rendering, physics, networking, and audio systems, is written in C++. Platform Infrastructure: C++ may be used for developing and optimizing various platform components, such as the Roblox client application, server software, and backend systems.

Can u get banned for scripting in Roblox? ›

roblox sees rules as black and white and sadly no matter the context if a rule is broken they will issue bans. if you were to just script your own it would be more similar to an admin panel and would be perfectly fine.

Are Roblox exploits illegal? ›

Exploiting or cheating is unfair to all Roblox players and creates a poor experience for everyone. These actions are a violation of the Roblox Terms of Use, and will lead to the deletion of an account.

How many scripts is too many Roblox? ›

It depends on what type of game it is. If it's a big game with lots of interactions, then it may be enough but if it's a game without a lot of interactions then yes, it is a lot. Edit: It probably will make your game laggy if there are many events happening at the same time.

What is require() in Roblox? ›

By itself, a module script can't run code — it needs to be loaded in another script using the keyword require(). The function require() accepts one argument, the location of the module script in the Explorer. To use a module, in a separate script, set a variable equal to require(moduleScript).

Are module scripts secure Roblox? ›

if the module script is inside server-script service/server-storage it cannot be accessed by the client and is inaccessible by exploiters. It is fine to do it.

What is oop Roblox? ›

OOP stands for Object Orientated Programming and is a way of laying out code in a more friendly way whilst also keeping large projects organised.

Is _g safe in Roblox? ›

Its 100% secure on the server. However, you should not be using _G due to the issues of race conditions, Roblox loading order when it comes to scripts is undefined.

What is _version in Roblox? ›

string. A global variable (not a function) that holds a string containing the current interpreter version.

Why does Roblox only use Lua? ›

No, Roblox does not use Java. Lua is easier to use compared to Java. Lua doesn't use syntax that some children can struggle with. For example, there is no need to add a semi-colon at the end of every line of code in Lua, whereas you do in Java.

How long does it take to learn Roblox scripting? ›

Roblox employs Lua, and learning the fundamentals of Lua can take anywhere from a few days to a few weeks, depending on how much time you devote to it. The more you practice, the more quickly you will learn.

Is scripting harder than coding? ›

In general, scripting is often easier to learn than programming, as scripting languages typically have fewer syntax rules and conventions, and they may also provide a higher-level abstraction of programming, making it easier to accomplish common tasks with less code.

Is it hard to learn scripting? ›

Learning a scripting language is an excellent introduction to coding and programming. They are relatively easy to learn and can be an effective jumping-off point to pursue your hobbies or career interests further.

Is Lua scripting hard? ›

Absolutely not. Whilst every programming language has it's variations and tricky parts, Lua is one of the easier languages for a kid to learn.

Top Articles
Latest Posts
Article information

Author: Carlyn Walter

Last Updated:

Views: 5470

Rating: 5 / 5 (50 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Carlyn Walter

Birthday: 1996-01-03

Address: Suite 452 40815 Denyse Extensions, Sengermouth, OR 42374

Phone: +8501809515404

Job: Manufacturing Technician

Hobby: Table tennis, Archery, Vacation, Metal detecting, Yo-yoing, Crocheting, Creative writing

Introduction: My name is Carlyn Walter, I am a lively, glamorous, healthy, clean, powerful, calm, combative person who loves writing and wants to share my knowledge and understanding with you.