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

Module Script Help? [closed]

Asked by 8 years ago

I have a brief idea of what they are and how to use, but I don't know how to properly function them. I have a normal ServerScript, and I want it to call the the code in ModuleScript, for example ServerScript

-- ModuleScriptFunction

and so if you understand I just want the serverscript to call the modulescript to start running it's code.

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?

1 answer

Log in to vote
6
Answered by 8 years ago

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:

-- Server script
require(script:WaitForChild("ModuleScript"))

And module ...

-- Module script
print("This module was required!")

return true -- MUST be here (the return value can be anything. Even nil, just as long as you manually put it there).

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:

-- Module script
local module = {}

return module

If you tried to return something like this, however...

-- Module script
local a,b,c = 1,2,3

return a,b,c

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:

local Test = require(338115513) -- "338115513" being your module ID.

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:

-- Module script
-- Basic table library with 2 functions
local Table = {}

function Table.copy(tab)
    local new = {}
    for i,v in next, tab do
        new[i] = v
    end
end

function Table.clear(tab)
    for i,v in next, tab do
        tab[i] = nil
    end
end

-- This metatable basically just makes it so this module extends the
-- built-in table library given to us by Lua.
return setmetatable(Table,{__index = table})

Required from...

-- Server script
-- Assuming the module script above is named "ModuleScript" and is inside this script

local table = require(script:WaitForChild("ModuleScript"))

print(table.copy) -- > the copy function from our library
print(table.clear) -- > the clear function from our library
print(table.insert) -- >  the insert function from the default library
print(table.remove) -- > the remove function from the default library

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

1
Great answer. Very detailed. M39a9am3R 3210 — 8y
Ad