ModuleScript

Show Deprecated

A ModuleScript is a script type that returns exactly one value by a call to require(). ModuleScripts run once and only once per Lua environment and return the exact same value for subsequent calls to require().

ModuleScripts are essential objects for adhering to the "Don't Repeat Yourself" (DRY) principle, allowing you to write a function only once and use it everywhere. Having multiple copies of a function is problematic when you need to change their behavior, so you should define functions or groups of functions in ModuleScripts and have your Scripts and LocalScripts call require() on those modules.

It's important to know that return values from ModuleScripts are independent with regards to Scripts and LocalScripts, and other environments like the Command Bar. Using require() on a ModuleScript in a LocalScript will run the code on the client, even if a Script did so already on the server. Therefore, be careful if you're using a ModuleScript on the client and server at the same time, or debugging it within Studio.

Note that the first call to require() will not yield (halt) unless the ModuleScript yields (calls task.wait() for example), in which case the current thread that called require() will yield until the ModuleScript returns a value. If a ModuleScript is attempting to require() another ModuleScript that in turn tries to require() it, the thread will hang and never halt (cyclic require() calls do not generate errors). Be mindful of your module dependencies in large projects!

If a ModuleScript is uploaded to Roblox and the root module has the name set to MainModule, it can be uploaded as a model and required using require() with the model's asset ID. Then it can be loaded into your experience, although this logic only works on the server and will error on the client. If other users want to use the module, it must be public.

Code Samples

The code sample starts by creating a local variable holding an empty table. It then fills the table with two placeholder functions, and then finally returns the table. Using this code in a ModuleScript would make the my_functions table (and thus any functions, tables or other values within it) available to any Script, LocalScript or other ModuleScript.

Simple ModuleScript Example

-- Tables store multiple values in one variable
local MyFunctions = {}
-- Add a few functions to the table
function MyFunctions.foo()
print("Foo!")
end
function MyFunctions.bar()
print("Bar!")
end
-- ModuleScripts must return exactly one value
return MyFunctions

This code sample shows how to use the require function on a ModuleScript, then use the value that it returned. See the "Simple ModuleScript Example" for the code to go with this sample.

Simple ModuleScript Usage

-- The require function is provided a ModuleScript, then runs
-- the code, waiting until it returns a singular value.
local MyFunctions = require(script.Parent.MyFunctions)
-- These are some dummy functions defined in another code sample
MyFunctions.foo()
MyFunctions.bar()

Properties

Source

Read Parallel
Plugin Security
OpenCloud Security

The code to be executed.

If you want to read or modify a script that the user has open, consider using the ScriptEditorService to interact with the Script Editor instead.

Methods

Events