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

What is a Module script?

I hear is is basicly a place to store a function to use in other scripts, but that's all I know. How do I use module scripts? I don't understand what the wiki means about them...

2 answers

✓ Accepted answer

Think of Module scripts as scripts with functions that can be used anywhere. I use them in most of my games, and inside of them I include functions that I will constantly be using throughout the development of a game. For example, lets say that I am making a game that constantly counts down from a certain number:

MyModuleScript:

local functions = {}

functions.countDown = function(start)
	for i = start,0,-1 do
		print(i)
	end
end)

functions.countUp = function(finish)
	for i = 0,finishdo
		print(i)
	end
end)

return functions

Instead of constantly having to write that function, you would just call the one from your module script

Script somewhere in your game:

local functions  = require(game.Workspace.MyModuleScript)

--Whatever you named the Module script would be what's inside the parentheses
--You can name "functions" whatever you want

functions.countDown(5)
print("------")
functions.countUp(5)

The output would be:

5
4
3
2
1
0
------
0
1
2
3
4
5

Module scripts are super helpful for big games that repeat a lot of tasks. If you have any questions feel free to ask!

2 comments

0

So would script.Parent work using one of these? Mabye I have script in a part that wants to call a module in workspace to do script.Parent.Transparency = 1?

0

No that wouldn't work. Only if the module script was in the the part you want to change the transparency for, but that would defeat the purpose of using a module script.

Modulescripts are simple. ModuleScripts do not function like normal scripts do. Instead they can be called using the require() function. Here's an example using ModuleScripts:

local module = {}
--This code is inside the modulescript in the workspace
function module:Test()
print("1-2-3 Test!")
end
return module
--In Script in serverscriptstorage
local Module = require(workspace.ModuleScript)
Module:Test() --> 1-2-3 Test!

If this didn't help, more info can be found here.

2 comments

0

Does the script have to be in serverscriptstorage? And that does the local module = {} line mean?

0

Module scripts do not need to be in ServerScriptService, they can be in any place a script can work, or even uploaded to ROBLOX and required from the assetid.

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