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:

    -- Tables store multiple values in one variable
    local my_functions = {}

    -- Add a few functions to the table
    function my_functions.foo()
        print("Foo!")
    end

    function my_functions.bar()
        print("Bar!")
    end

    -- ModuleScripts must return exactly one value
    return my_functions

Server:

    -- 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()

Expected Output:

Foo!
Bar!

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

Ad

Answer this question