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

What are module scripts? What are they used for?

Asked by 5 years ago
Edited 5 years ago

So, I was just clicking random buttons and then I found module scripts. Can anyone explain to me what they are and what do we use them for?

0
In a nutshell, it lets you share functions/etc so you don't have to write them all over again between scripts. https://wiki.roblox.com/index.php?title=API:Class/ModuleScript ScrewDeath 153 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago

A module script is a script that contains functions or variables that you can use in multiple scripts, by requiring them. For example:

module:

local module = {} ---------module can be anything, but most of the time it is a table

function module.printsomething(A)
    print(A)
    print("Module script!")
end

return module ---------basically the table gets returned when you require() this module

script:

local module = require(workspace.Module) -----or wherever your module is located

module.printsomething("hello world!")

The script outputs: hello world! Module script!

0
I forgot to add that you can store multiple stuff in a module. Every time you call a module, it returns whatever the "return" inside it points to, so you can tables containing information about items, so you don't have to copy the table with the items in every script you need them. radusavin366 617 — 5y
0
Is there a way to make it local somehow? mixgingengerina10 223 — 5y
0
no, to use it from a local script you need to place it inside one of the repl storages, if you require it from a local script itll be able to do local stuff like get the localplayer etc. radusavin366 617 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

A Module Script is basically a replacement for the Global Functions which for some reason isn't deprecated yet... But they basically allow you to run the same bit of code across multiple scripts / local scripts / modules.

To reference one...

local ModuleScript = require(game.ServerScriptService.ModuleScript)

Module scripts are almost like classes meaning that you can have variables and functions inside them...

local module = {}

module.variable = 5

module.doesNothing = function(val)
    val = val - val + val
    return val
end

return module

You can call the functions and variables like so...

local ModuleScript = require(game.ServerScriptService.ModuleScript)

print(ModuleScript.doesNothing(10))

print(ModuleScript.variable)

Note, a module script does not run ANY code until it is required. This means that you can't have events such as... game.Players.PlayerAdded:Connect(function(player) ... end)

Good uses for them include the ability to run the same code from anywhere on the Module Script's respective side (server or client). The ability to house libraries and data to reference from anywhere.

Example

I have a lot of tools in my game, but I don't want to make a code block for each of the tools to equip it. So I make a Module Script that I can pass the tool info to, and it positions and equips it on my player.

Helpful Link: https://wiki.roblox.com/index.php?title=API:Class/ModuleScript

0
Is there a way I can use game.Players.LocalPlayer in them? mixgingengerina10 223 — 5y
0
I don't believe so, though you can pass players into the module function's parameters. climethestair 1663 — 5y

Answer this question