ModuleScript is a Script fragment. It only runs when a Script uses require() on it. The class was added in version 0.131.
Function[]
ModuleScripts are ran using require(module). Modules must return something, which is the return value of the require function. All code is ran in the module when called and the return type is returned. This is usually a table of properties, functions, and events, or a function which does something to the game. However, the return type can be anything, including nil.
Private Modules[]
If a ModuleScript is renamed to "MainModule" and uploaded as a Roblox model, it can be called by using require([model id]). This is commonly used to create free models which contain code that is private, or is not wanted being shared by the owner. On December 5, 2018, Seranok announced the removal of this feature to be on February 1, 2019. The removal was delayed until February 25, when the feature was finally removed.
The following example uses private modules.
Code in MainModule:
local module = {}
module.Code = 10
return module
MainModule is published to Roblox and given the id of '187' Code in Script:
local module = require(187)
print(module.Code) --> Prints 10
Notice how the code inside the module script cannot be retrieved, because a table is returned instead of the script being returned.
Properties
Source: ProtectedString
- Category: Data.
- Serialization: can save and load.
- Thread safety: read safe