Modules!
They're a wonderful thing. They give us the ability to create our own libraries in an organized fashion, or just to run arbitrary code from a new script environment.
Rules?
One of the beautiful things about modules, is their simplicity. They function the exact same way any script would, with a few exceptions.
What does this mean? Basically, a module script acts as a function. You're able to have code run inside of it, and return a value to whatever 'called' it (required, in this case). However, there are a few differences:
1: You MUST return a value (even if you don't use it)
Let's say you have a module inside a server script:
2 | require(script:WaitForChild( "ModuleScript" )) |
And module ...
2 | print ( "This module was required!" ) |
Without this return value, you get the infamous error message:
05:45:51.531 - Module code did not return exactly one value
05:45:51.532 - Requested module experienced an error while loading
2: You can only return 1 value
The second difference between normal functions and modules, is that your module script can only return 1 value. This is why it's more than common to see modules return tables than anything else, because sometimes you want an entire library of things you can use rather than just having it return 1 value. This is also why you get the code pre-written in each module that looks like this:
If you tried to return something like this, however...
That will error and return the same error message as the one you see when not returning anything at all.
Require with just a module ID?
Yep, it's possible! If you save a module script to your models, get the item's ID found at the end of the URL (location: http://prntscr.com/9km1zx), and pass it as a parameter of require
, this will also work. BUT, in order for the module to successfully import, you MUST name the physical module script object "MainModule", then save it.
One that's done, you can require a module just by using it's model ID with this format:
1 | local Test = require( 338115513 ) |
Because this is getting information outside the ROBLOX server, it'll take a few tenths of a second to retrieve. But this shouldn't be a problem.
Suggestion?
Anyway, back to return values.
I personally suggest always returning a table (that is, if you're intent is actually creating a library to save your main script from getting cluttered with functions), so you can return as many values as you want all nice and packed in a handy, portable table. Here's an example of how you could create a library using a module script:
05 | function Table.copy(tab) |
07 | for i,v in next , tab do |
12 | function Table.clear(tab) |
13 | for i,v in next , tab do |
20 | return setmetatable (Table, { __index = table } ) |
Required from...
4 | local table = require(script:WaitForChild( "ModuleScript" )) |
Hope this helped.
Side note:
You should also keep in mind, that module scripts run neither server-sided, or client-sided. But rather, on the side of whatever kind of script required it. For instance, if you require a module script from a local script, the code inside the module will run locally.
Vise-versa, if you require the module from a server script, the code in the module will run server-sided.
Storing them?
If you'd like to get an idea of where to store your module scripts in-game, you can take a look at this layout of a project I'm working on: http://prntscr.com/9klzmc
Locked by NinjoOnline, MasterDaniel, Necrorave, and ChemicalHex
This question has been locked to preserve its current state and prevent spam and unwanted comments and answers.
Why was this question closed?