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

What are ModuleScripts?

Asked by 9 years ago

What are they, and what are they used for?

0
I had asked the same question a few months back and got a rather nice answer. Please visit: https://scriptinghelpers.org/questions/15474/ FearMeIAmLag 1161 — 9y

1 answer

Log in to vote
0
Answered by
davness 376 Moderation Voter
9 years ago

Let's look at a game.

Imagine that you have several scripts in the same game:

-- script 1
function doStuff()
    local p = Instance.new("Part", workspace)
    p.BrickColor = BrickColor.random()
    return p
end

doStuff()
-- script 2

function doStuff()
    local p = Instance.new("Part", workspace)
    p.BrickColor = BrickColor.random()
    return p
end

doStuff()
-- script 3

function doStuff()
    local p = Instance.new("Part", workspace)
    p.BrickColor = BrickColor.random()
    return p
end

doStuff()
-- script 4

function doStuff()
    local p = Instance.new("Part", workspace)
    p.BrickColor = BrickColor.random()
    return p
end

doStuff()

I am exaggerating, but isn't boring to copy and paste the same function to all scripts that need it?

The module script is used to reduce the lines you should write.

When you insert a module script, it looks like this:

local module = {}

return module

Now it's time to insert the code on the module script.

local module = {}
    function module.doStuff()
        local p = Instance.new("Part", workspace)
        p.BrickColor = BrickColor.random()
        return p
    end
return module

To make a script use the module content, you should use a require keyword:

local moduleStuff = require(--[[the location of the module script]])
moduleStuff.doStuff() --calls the function.

so, ModuleScriptsare just made to help us be more comfortable at the scripting.

Ad

Answer this question