Class
ModuleScript
A script type that runs once when LuaGlobals.require() is called with
it. Returns exactly one value, usually a table of functions, to used by other
scripts. Useful for compartmentalizing code.
A ModuleScript is a script type that returns exactly one value by a
call to LuaGlobals.require(). ModuleScripts run
once and only once per Luau environment and return the exact same value for
subsequent calls to LuaGlobals.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
LuaGlobals.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
LuaGlobals.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 LuaGlobals.require() will not yield
(halt) unless the ModuleScript yields (calls task.wait() for
example), in which case the current thread that called
LuaGlobals.require() will yield until the ModuleScript
returns a value. If a ModuleScript is attempting to
LuaGlobals.require() another ModuleScript that in turn tries
to LuaGlobals.require() it, the thread will hang and never halt
(cyclic LuaGlobals.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
LuaGlobals.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.
Properties 2#
LinkedSourceContentId | Used to store a URL that points to an online script source. Binds the
online code to the script's Script.Source.ReadSafeDeprecated |
SourceProtectedString | The code to be executed.ReadSafe |
LinkedSource: ContentId#
DeprecatedReadSafeDeprecated
Deprecated. This property is now replaced by packages which has greater functionality.
Used to store a URL that points to an online script source. Binds the
online code to the script's Script.Source.
Source: ProtectedString#
ReadSafe
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.
Inherited members#
Inherited from Instance 58
Properties (10)
Archivable, archivable, Capabilities, IsInSandbox, Name, Parent, PredictionMode, RobloxLocked, Sandboxed, UniqueId
Methods (39)
AddTag, children, ClearAllChildren, Clone, clone, Destroy, destroy, FindFirstAncestor, FindFirstAncestorOfClass, FindFirstAncestorWhichIsA, FindFirstChild, findFirstChild, FindFirstChildOfClass, FindFirstChildWhichIsA, FindFirstDescendant, GetActor, GetAttribute, GetAttributeChangedSignal, GetAttributes, GetChildren, getChildren, GetDebugId, GetDescendants, GetFullName, GetStyled, GetStyledPropertyChangedSignal, GetTags, HasTag, IsAncestorOf, IsDescendantOf, isDescendantOf, IsPropertyModified, QueryDescendants, Remove, remove, RemoveTag, ResetPropertyToDefault, SetAttribute, WaitForChild