PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

ModuleScript

Show deprecated

ModuleScript

Show deprecated

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 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 requires 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 object is has its Name property set to ‘MainModule’ and is uploaded to Roblox as a model to your account, Scripts can use require with the uploaded model’s AssetId instead. This allows you to create private modules on your Roblox account!

Properties

Content

LinkedSource

[Deprecated]

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

ProtectedString

Source

The code to be executed.

Inherited from LuaSourceContainer: Show Hide

Instance

CurrentEditor

[NotScriptable]

The Player who is currently editing this script. This property is only used when in Team Create mode.

Inherited from Instance: Show Hide

bool

Archivable

Determines if an Instance can be cloned using /Instance/Clone or saved to file.

string

ClassName

[ReadOnly] [NotReplicated]

A read-only string representing the class this Instance belongs to

int

DataCost

[ReadOnly] [NotReplicated] [Deprecated]

The cost of saving the instance using data persistence.

string

Name

A non-unique identifier of the Instance

Instance

Parent

Determines the hierarchical parent of the Instance

bool

RobloxLocked

If true, the Instance and its descendants cannot be indexed or edited by a Script or LocalScript and will throw an error if it is attempted

bool

archivable

[Hidden] [NotReplicated] [Deprecated]
string

className

[ReadOnly] [NotReplicated] [Deprecated]

Functions

Inherited from Instance: Show Hide

void

ClearAllChildren ( )

This function destroys all of an Instance's children.

Instance

Clone ( )

Create a deep copy of a Roblox instance and descendants where Archivable = true.

void

Destroy ( )

Sets the Instance/Parent property to nil, locks the Instance/Parent property, disconnects all connections and calls Destroy on all children.

Instance

FindFirstAncestor ( string name )

Returns the first ancestor of the Instance whose Instance/Name is equal to the given name.

Instance

FindFirstAncestorOfClass ( string className )

Returns the first ancestor of the Instance whose Instance/ClassName is equal to the given className.

Instance

FindFirstAncestorWhichIsA ( string className )

Returns the first ancestor of the Instance for whom Instance/IsA returns true for the given className.

Instance

FindFirstChild ( string name , bool recursive )

Returns the first child of the Instance found with the given name.

Instance

FindFirstChildOfClass ( string className )

Returns the first child of the Instance whose Instance/ClassName|ClassName is equal to the given className.

Instance

FindFirstChildWhichIsA ( string className , bool recursive )

Returns the first child of the Instance for whom Instance/IsA returns true for the given className.

Variant

GetAttribute ( string attribute )

RBXScriptSignal

GetAttributeChangedSignal ( string attribute )

Dictionary

GetAttributes ( )

Objects

GetChildren ( )

Returns an array containing all of the Instance's children.

string

GetDebugId ( int scopeLength )

[NotBrowsable]

Returns a coded string of the Instances DebugId used internally by Roblox.

Array

GetDescendants ( )

[CustomLuaState]

Returns an array containing all of the descendants of the instance

string

GetFullName ( )

Returns a string describing the Instance's ancestry.

RBXScriptSignal

GetPropertyChangedSignal ( string property )

Get an event that fires when a given property of an object changes.

bool

IsA ( string className )

[CustomLuaState]

Returns true if an Instance's class matches or inherits from a given class

bool

IsAncestorOf ( Instance descendant )

Returns true if an Instance is an ancestor of the given descendant.

bool

IsDescendantOf ( Instance ancestor )

Returns true if an Instance is a descendant of the given ancestor.

void

Remove ( )

[Deprecated]

Sets the object’s Parent to nil, and does the same for all its descendants.

void

SetAttribute ( string attribute , Variant value )

Instance

WaitForChild ( string childName , double timeOut )

[CustomLuaState] [CanYield]

Returns the child of the Instance with the given name. If the child does not exist, it will yield the current thread until it does.

Objects

children ( )

[Deprecated]

Returns an array of the object’s children.

Instance

clone ( )

[Deprecated]
void

destroy ( )

[Deprecated]
Instance

findFirstChild ( string name , bool recursive )

[Deprecated]
Objects

getChildren ( )

[Deprecated]
bool

isA ( string className )

[Deprecated] [CustomLuaState]
bool

isDescendantOf ( Instance ancestor )

[Deprecated]
void

remove ( )

[Deprecated]

Events

Inherited from Instance: Show Hide

RBXScriptSignal

AncestryChanged ( Instance child , Instance parent )

Fires when the Instance/Parent property of the object or one of its ancestors is changed.

RBXScriptSignal

AttributeChanged ( string attribute )

RBXScriptSignal

Changed ( string property )

Fired immediately after a property of an object changes.

RBXScriptSignal

ChildAdded ( Instance child )

Fires when an object is parented to this Instance.

RBXScriptSignal

ChildRemoved ( Instance child )

Fires when a child is removed from this Instance.

RBXScriptSignal

DescendantAdded ( Instance descendant )

Fires when a descendant is added to the Instance

RBXScriptSignal

DescendantRemoving ( Instance descendant )

Fires immediately before a descendant of the Instance is removed.

RBXScriptSignal

childAdded ( Instance child )

[Deprecated]

Code Samples


Simple ModuleScript Example

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.

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

Simple ModuleScript Usage

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.

    -- The require function is provided a ModuleScript, then runs
    -- the code, waiting until it returns a singular value.
    local my_functions = require(script.Parent.MyFunctions)

    -- These are some dummy functions defined in another code sample
    my_functions.foo()
    my_functions.bar()