Are there any exercises to help me understand ModuleScripts?

I came to ask if there are like any exercises or something I can do to help me understand ModuleScripts. I know someone is going to comment just look up how to use ModuleScripts but when I do and I read the replies and try to understand what is going on, nothing makes sense. So back to the question: Is there anything I can do to help myself understand or could someone dumb it down a lot because they make absolutely no sense to me and I honestly need the help.

In the bottom of this article, there’s an example:

2 Likes

Basics:
Like scripts, module scripts run code. However, unlike scripts, modulescripts only run when they are required for the first time and they return a value. That can be any value, but it’s usually a table containing functions or a function.

Requiring:
Requiring a module script means using the require() function and giving the Modulescript instance as an argument. The require function returns the value that the ModuleScript returns.

As mentioned before, the modulescript runs only when it’s required for the first time. After that, the value is cached and later require calls will return the cached value, which means that a spesific modulescript always returns the same value.

Other:
Any functions stored in a table returned by a module script can be called in the script (or another modulescript) that requires the modulescript.

The functions returned by a modulescript can use variables that are defined in the modulescript, because they run in the environment of the modulescript even if they are called from elsewhere. They can not use variables defined in the script/modulescript requiring them. But of course the functions can have parameters for values they need to get from the script/modulescript calling them.

Although modulescripts only run once, the variables inside them can be changed after they have finished running, because the functions they return might change the values of those variables.

Use:
Modulescripts are extremely useful. They can be used to store functions or other values that are needed by many scripts or modulescripts. They can also be used to break a long script into many shorter ones that are easier to understand and edit. One use of module scripts is making your own classes, like an own event class.

Modulescript example:

local returnValues = {}

local myNum= 45 -- this can't be directly accessed from script requiribg the module
returnValues.MyNum = 32 -- this can be accessed from the script requiring the module.

-- this function makes it possible to get the value of myNum in another script.
function returnValues.GetMyNum()
    return myNum
end


-- the value of multiplier is given as an argument by the script requiring the module.
function returnValues.GetMultipliedMyNum(multiplier)
    return myNum*multiplier
end


-- this function can't be directly accessed by a script requiring the module, but it can be accessed by functions returned by the module.
local function getModuleName()
    return script.Name
end

function returnValues.GetLowerCaseModuleName()
    return getModuleName():lower()
end

return returnValues

Module scripts are like scripts but their main function is to return a value. This is a very simple one-line module script:

return 5 -- it returns the number 5, that's all it does.

Now if you want another script to get the value that a module script returns, you would do require() and then pass the module script as the parameter.

For example, we have a module script in replicated storage named “Test”, and this is what’s in it.

return "This is the value I am returning"

Now we have a server script that gets this module script’s value.

local ModuleScriptsValue = require(game.ReplicatedStorage.Test)
print (ModuleScriptsValue) -- It would print "This is the value I am returning"

Let’s say you have a large 100 line or so function that does some stuff and a lot of different scripts use that function, instead of copy and pasting it in every script that uses it, why not just store it in a module script and have the scripts just require the module script to get the function?

Module script:

function foo()
    -- the stuff the function does
end

return foo

ServerScript:

local TheFunction  = require(ModuleScript)
TheFunction()