Reference API Roblox

Engine API

Website

Related

Reference API Roblox

ModuleScript

A script type 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.

Member index 3

HistoryMember
645LinkedSource: ContentId
646Source: ProtectedString
inherited from LuaSourceContainer
inherited from Instance
553Archivable: bool
635Capabilities: SecurityCapabilities
553Name: string
553Parent: Instance
635Sandboxed: bool
616UniqueId: UniqueId
576AddTag(tag: string): null
573ClearAllChildren(): null
462Clone(): Instance
573Destroy(): null
486FindFirstAncestor(name: string): Instance
486FindFirstAncestorOfClass(className: string): Instance
486FindFirstAncestorWhichIsA(className: string): Instance
486FindFirstChild(name: string, recursive: bool = false): Instance
486FindFirstChildOfClass(className: string): Instance
486FindFirstChildWhichIsA(className: string, recursive: bool = false): Instance
486FindFirstDescendant(name: string): Instance
563GetActor(): Actor
486GetAttribute(attribute: string): Variant
462GetAttributeChangedSignal(attribute: string): RBXScriptSignal
631GetAttributes(): Dictionary
648GetChildren(): Instances
462GetDebugId(scopeLength: int = 4): string
486GetDescendants(): Array
486GetFullName(): string
641GetStyled(name: string): Variant
576GetTags(): Array
576HasTag(tag: string): bool
486IsAncestorOf(descendant: Instance): bool
486IsDescendantOf(ancestor: Instance): bool
580IsPropertyModified(name: string): bool
573Remove(): null
576RemoveTag(tag: string): null
580ResetPropertyToDefault(name: string): null
573SetAttribute(attribute: string, value: Variant): null
462WaitForChild(childName: string, timeOut: double): Instance
648children(): Instances
553clone(): Instance
573destroy(): null
553findFirstChild(name: string, recursive: bool = false): Instance
648getChildren(): Instances
553isDescendantOf(ancestor: Instance): bool
573remove(): null
462AncestryChanged(child: Instance, parent: Instance)
462AttributeChanged(attribute: string)
462ChildAdded(child: Instance)
462ChildRemoved(child: Instance)
462DescendantAdded(descendant: Instance)
462DescendantRemoving(descendant: Instance)
500Destroying()
553childAdded(child: Instance)
inherited from Object
647ClassName: string
647className: string
647GetPropertyChangedSignal(property: string): RBXScriptSignal
647IsA(className: string): bool
650isA(className: string): bool
647Changed(property: string)

Removed member index 1

HistoryMember
428CoreScriptModified: bool

Description

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.

History 19

Members 3

LinkedSource

TypeDefault
ContentId

Used to store a URL that points to an online script source. Binds the online code to the script's Script.Source.

This property is deprecated. It exists only for backward compatibility, and should not be used for new work.

History 5

Tags: [Deprecated]

Source

TypeDefault
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.

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

TypeDefault
bool
This property is not scriptable. It cannot be accessed by script code.
This property is not replicated. Its interface does not cross the network boundary.

History 2

Tags: [NotReplicated, NotScriptable]

Settings