Do module scripts help the game be less laggy?
ModuleScripts
are essentially a neat and organized way to create libraries
for your code. Modules work very similar to how ordinary functions
work, despite a few minor exceptions:
21:22:37.741 - Module code did not return exactly one value
21:22:37.858 - Requested module experienced an error while loading
Return limit Modules can only return one value
at a time. This is why quite often you'll see module scripts returntables
consisting of API functions, instead of just returning a single function or other data type. If you do return more than one value at a time, you'll get the same error message as shown above when requiring it.
Requiring your module Just like functions, modules must be activated, or "called" in order to run it's source. This is done via the require
function.
What is 'require'?
The require
function consists of one argument
, which can be one of two things:
The module script object (userdata
, the actual script directory)
A module's item Id (number
). This Id is found at the end of a URL of a published module script. Note, that if you are to require a module via it's module Id, the name of the module must be called "MainModule
". If this requirement is not met, your code will error upon loading the module.
Implementing modules
Modules are used to shorten code, by taking care of annoying tasks outside of the visual script editor, to leave room for what's really important - the program's logic.
For example, let's say I wanted a script to read and write data to a table stored in _G
, every 10 seconds. Instead of having all that code written in the main script, I could make a module with some methods called "Read" and "Write" that we can call from our required module.
ModuleScript
local Module = {} -- what we return to the require function call if not _G.data then -- Create the data table _G.data = {} end -- Module's API function Module:Read(v) print(_G.data[v]) end function Module:Write(i,v) _G.data[i] = v end return Module
Main script
-- We'll assume the module above is in the ReplicatedStorage local Module = require(game:GetService("ReplicatedStorage"):WaitForChild("Module")) while wait(10) do Module:Write("Test",math.random()) -- Calling method from the module, storing a random number in it's "Test" key Module:Read("Test") end
And that's basically it. The example I gave above is no more than calling a function from inside a table, the only difference being the table was given to us by the required module.
Hope this helped, just let me know if you have any questions.
Module Scripts are used to hold functions and variables to be used by other scripts. They can even be used to run functions for multiple games if required as an asset. This makes it easier for you to make a change, and have it carry over to the other games.
When required, module scripts will execute the code within them and return a preferred value. If a module script is required twice in the same environment, it will return the previous result unless it is a local script.
You might see module scripts take the place of settings, while other times you might see the require function be the only thing in a script. Many people use module scripts as a settings script to make sure they are contained and users don't go messing around with the actual hard code too much. Other times people will upload the module script and provide a free model to require that module script from the site, as to keep their source code concealed.
Within a new module script, you will see the following code...
local module = { } return module
At this point you can rename module to anything you want and modify the code to your preferences. In a script you would then be able to require the module script. Just a note to be taken, whatever type of script you use to require the module is the type of environment your script will take after. So if you use a Server Sided Script, your module will run server sided. If you require it locally, it will run locally.
Now for requiring. If you want to upload the module to the Roblox website, you need to make sure the module's name is MainModule. This will allow you to run the code in the module script in game and keep the code of the module hidden. For running the module script, you need to use the require function on the instance or asset Id of the module.
require(12345678) --Running whatever is in the module script that was uploaded, which in this case is that table.
A module script has to return a value, but it can be a function, number, table, boolean or anything.
Here is some more information on Module Scripts.