ModuleScript
A type of script object that runs once when require
is called with it.
Returns exactly one value, usually a table of functions, to used by other
scripts. Useful for compartmentalizing code.
Memory category | Script |
---|
Member index 3
Removed member index 1
History | Member | |
---|---|---|
428 | CoreScriptModified: bool |
Description
A ModuleScript is a type of Lua source container that runs once and must
return exactly one value. This value is then returned by a call to require
given the ModuleScript as the only argument. 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. When you write a function, write it only once and use it
everywhere. Having multiple copies of a function is disastrous when you need
to change that behavior. So, you should define functions or groups of
functions in ModuleScripts and have your Scripts and LocalScripts call
require
on your ModuleScripts. Keep your code organized!
It's important to know that return values from ModuleScripts are independent
with regards to LocalScripts and Scripts, 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. Similarly,
in Roblox Studio, using require
on a ModuleScript in the hierarchy with the
Command Bar will give a similar behavior. So, be careful if you are using a
ModuleScript on the client and server at the same time, or debugging it within
Studio.
Note that the first call to require
on a ModuleScript will not yield (halt)
unless the ModuleScript yields (e.g. calls task.wait()). The current
thread that called require
will yield until a ModuleScript returns a value.
A run time error is generated if this doesn't happen. 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.
History 19
- 646 Change ReadSecurity of Source from PluginSecurity to None
- 646 Change WriteSecurity of Source from PluginSecurity to None
- 645 Change ValueType of LinkedSource from Content to ContentId
- 614 Change Tags of LinkedSource from [] to [Deprecated]
- 553 Add
- 486 Change ThreadSafety of Source from ReadOnly to ReadSafe
- 486 Change ThreadSafety of LinkedSource from ReadOnly to ReadSafe
- 462 Change ThreadSafety of Source from to ReadOnly
- 462 Change ThreadSafety of LinkedSource from to ReadOnly
- 428 Remove CoreScriptModified
- 410 Add CoreScriptModified
- 187 Add LinkedSource
- 187 Change Superclass of ModuleScript from Instance to LuaSourceContainer
- 151 Change WriteSecurity of Source from security1 to PluginSecurity
- 151 Change ReadSecurity of Source from security1 to PluginSecurity
- 150 Change WriteSecurity of Source from LocalUserSecurity to security1
- 150 Change ReadSecurity of Source from LocalUserSecurity to security1
- 137 Add Source
- 131 Add ModuleScript
Members 3
LinkedSource
Type | Default | |
---|---|---|
ContentId |
Used to store a URL that points to an online script source. Binds the online code to the script's Script.Source.
Thread safety | ReadSafe |
---|---|
Category | Data |
Loaded/Saved | true |
History 5
- 645 Change ValueType of LinkedSource from Content to ContentId
- 614 Change Tags of LinkedSource from [] to [Deprecated]
- 486 Change ThreadSafety of LinkedSource from ReadOnly to ReadSafe
- 462 Change ThreadSafety of LinkedSource from to ReadOnly
- 187 Add LinkedSource
Source
Type | Default | |
---|---|---|
ProtectedString |
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.
Thread safety | ReadSafe |
---|---|
Category | Data |
Loaded/Saved | true |
History 9
- 646 Change ReadSecurity of Source from PluginSecurity to None
- 646 Change WriteSecurity of Source from PluginSecurity to None
- 486 Change ThreadSafety of Source from ReadOnly to ReadSafe
- 462 Change ThreadSafety of Source from to ReadOnly
- 151 Change WriteSecurity of Source from security1 to PluginSecurity
- 151 Change ReadSecurity of Source from security1 to PluginSecurity
- 150 Change WriteSecurity of Source from LocalUserSecurity to security1
- 150 Change ReadSecurity of Source from LocalUserSecurity to security1
- 137 Add Source
Removed members 1
CoreScriptModified
Type | Default | |
---|---|---|
bool |
Security | RobloxSecurity |
---|---|
Category | Data |
Loaded/Saved | false |
History 2
- 428 Remove CoreScriptModified
- 410 Add CoreScriptModified