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

What are module scripts for?

Asked by 8 years ago

Do module scripts help the game be less laggy?

2 answers

Log in to vote
4
Answered by 8 years ago

Introduction


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:

  • Return Module scripts must return a value at the end of it's code, no matter what. If this requirement isn't met, you get this infamous error message:

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.

0
God, this is so much to take in... GeezuzFusion 200 — 8y
0
If you want, I could make a YouTube video explaining it in further detail. ScriptGuider 5640 — 8y
0
Have you ever used a Module Script? GeezuzFusion 200 — 8y
0
All the time. They're great for organization and portable code. Here's how I use them: http://prntscr.com/ag8ey2 ScriptGuider 5640 — 8y
View all comments (2 more)
0
Yes, please. I really want to get into this module scripting. GeezuzFusion 200 — 8y
0
Alright. I'll be able to upload a video on it this week. I'll let you know when it's uploaded. ScriptGuider 5640 — 8y
Ad
Log in to vote
1
Answered by
M39a9am3R 3210 Moderation Voter Community Moderator
8 years ago

Basic Explanation

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.

Typical Usage

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.

How to Set-up

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.

If you have any questions, feel free to comment below. If this answered your question, please hit the accept answer button. Hopefully this helped you out.

Answer this question