Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Can somebody tell me how module scripts are suppose to work? It is very confusing to understand.

Asked by
QWJKZXF 88
4 years ago

I never seem to understand the use of the module scripts. Some examples have shown that they are used as dictionaries that can be called by multiple other scripts at once. Others are just scripts themselves that are just called by other scripts.

For example, in Roblox's gun kits, normal scripts call on a module script.

Can somebody please explain how these module scripts are supposed to function, unlike a normal or local script?

1 answer

Log in to vote
1
Answered by 4 years ago
Edited 4 years ago

A module script is a container that only runs once, and returns a value. You can get that value by using a require() on the module script. They run on the client, and can be called by the server. Here's an example:

Module:

01-- Tables store multiple values in one variable
02local my_functions = {}
03 
04-- Add a few functions to the table
05function my_functions.foo()
06    print("Foo!")
07end
08 
09function my_functions.bar()
10    print("Bar!")
11end
12 
13-- ModuleScripts must return exactly one value
14return my_functions

Server:

1-- The require function is provided a ModuleScript, then runs
2-- the code, waiting until it returns a singular value.
3local my_functions = require(script.Parent.MyFunctions)
4 
5-- These are some dummy functions defined in another code sample
6my_functions.foo()
7my_functions.bar()

Expected Output:

1Foo!
2Bar!

Here's the full documentation: https://developer.roblox.com/en-us/api-reference/class/ModuleScript

Ad

Answer this question