How can I work with Module Scripts?

How can I work with Module Scripts? When I do need to use them and how can I use that the more efficient way? Thank you in advance! I am kinda new at programming and I need even more knowledge, thank you!

5 Likes

Module Scripts are helpful because their Modular. They can be configured very easily. Server Scripts and Local Scripts are very open, like one error breaks the whole script. A Module Script can have a function where it opens a door, and if there is a error you can fix it really easily as if in a Script you have to rewrite things. And it’s more heavier.

Overall Module Scripts are supposed to keep your scripts organized and clean. Use module scripts when making a Main Game Script, like for rounds. Or for doors.

They make scripting really efficient when needing 2 scripts but want some to interact with each other.

5 Likes

Module Scripts allow for easy access to code

For example,
ProfileService. All in one module script, you have many functions to work with - Profile:Reconcile(), ProfileService.GetProfileStore()…

Let’s make a custom function for making a new part.
We can name our ModuleScript, “Part Tools”.
And as for the parent, ServerScriptService

ModuleScript:

local functions = {

CreatePart = function(amount)
local amountofparts = amount or 1

repeat amountofparts -= 1
local part = Instance.new("Part", workspace)
until amountofparts == 0
end

}

return functions

A server script:

local functions = require(game:GetService("ServerScriptService"):WaitForChild("Part Tools"))

functions.CreatePart(15) --Creates 15 parts inside workspace
functions.CreatePart() --Defaults to 1 as we set nil to 1 inside the function

And it’s not just functions, module scripts can be used as a table as well, a bit boring, but convenient.

Example:

local myFruits = {
banana = 10;
apple = 100;
}

return myFruits

Another Script:

local fruits = require(game:GetService("ServerScriptService"):WaitForChild("Fruits"))

function multiply(num1, num2)
return num1 * num2
end

print(multiply(fruits.banana, fruits.apple)) --Prints 1,000

NOTE: Module Scripts cannot run on their own, they need another script to call require() on them for them to run.

3 Likes

Thank you! I am making a game that requires a module, I did it though I didn’t use module, I will try to make each function on there to the module!

1 Like

Thank you @remcodesremcodes, I will try my best working with it :smiley:.

1 Like

You know how Roblox has services like MarketplaceService and DataStoreService? Think of ModuleScripts like those; but you’re the one making the service!

ModuleScripts are scripts that contain a table of functions that can be required and called from other scripts.

For example, I’ve provided some code down below.

Server Script - Placed in the Workspace

local ServerScriptService = game:GetService("ServerScriptService")
local HelloService = require(ServerScriptService.HelloService)

HelloService:SayHello()

Module Script - Placed in ServerScriptService and named “HelloService”

local module = {} -- Can be renamed

function module:SayHello()
     print("Hello world!")
end

return module

I find modules helpful when I have multiple scripts that need to access the same function. Many creators additionally use them to make UI scripting easier.

You can also pass parameters through module functions!

Click here to learn more about modules!

1 Like

Thank you @blvecoves! I loved the example you made! I will make use that on my Menu that uses lot of TweenService! I am going to try add parameters which contains the information of the tween and some parts of the script! Thank you!

1 Like