How do I use modules?

Could someone help me find a good resource that explains modules I am trying to use one for my bullet hell game, but I keep getting weird errors.

https://education.roblox.com/en-us/resources/intro-to-module-scripts
There are plenty of roblox published resources on this. The main purpose of modulescripts is organisation. Other uses include object oriented programming and loading order.

1 Like

Module scripts are for sharing tables, in other words, a module script is a table. Usually that table contains functions, but it can all can contain numbers, strings, etc. Module scripts are “required” so other scripts can use them like this:

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

And the module script itself, may contain code like this:

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

Module scripts cannot run independently. A lot of times, people create resources in the form of module scripts for them to be required by other scripts to use that module’s functions.

1 Like