Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

What are module scripts? What are they used for?

2 answers

✓ Accepted answer

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!

3 comments

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.

0

Is there a way to make it local somehow?

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.

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

2 comments

0

Is there a way I can use game.Players.LocalPlayer in them?

0

I don't believe so, though you can pass players into the module function's parameters.

This discussion is preserved as it was. New answers, comments, and votes are closed.