What are Module Scripts?~~
Well, Module scripts are a special type of script roblox has given us to make coding easier
and less messy.
How do I find it?~~
You would insert it as you would for any other type of script. Right click and find it.
Where do I keep it?~~
You can keep it in any place that will replicate to the player. Places such as ReplicatedStorage,
StarterPlayer, StarterGUI and etc...
How do I use it?~~
In a local script or starter script, make a variable called module.
Module scripts have a special calling function that you must use to find it's directory.
Ex.
local module = require(game.Workspace:GetService("ReplicatedStorage"):WaitForChild("Module"))
So in this instance, I have kept the module script in the replicated storage where it's easily
found.
Now, go into the module script. You might see some code already there. That's basically a
table to keep your functions.
So, to make an actual function type this out: (I will explain as we go)
module = {}
function module:CreatePart()
end
return module
As you can see, it does look different from a regular function that you might write in a local
or server script. Don't worry we can break it down. So, how do you usually make a function?
You usually start with typing out function and the name you want to call it. Like so:
function functionName()
end
Well, it's bascially the same here but you can see that there's something between function and
the name. It's :module. So, why is that there? Well, think of it as another variable but you
won't be using it in the module script. You will be calling it from a local or server script.
This is beacuse as you make functions, you will need to call them somehow and using
module:FunctionName()
will help you. Now, in my example I want to create a part when I click a button. So, let's go back the local script and quickly make a script that will do so.
local partButton = script.Parent
partButton.MouseButton1Clicked:Connect(function()
end)
So basically when the button is clicked, it will trigger an event. With this we can now go back
into the module script and make a part.
module = {}
function module:CreatePart()
local part = Instance.new("Part",workspace)
end
return module
So, now that the function is created we need a way to actually make it happen.
How do we do this? Well, we can start by going back the local script and using a special feature
to call it. We are also going to keep the variable so we can call it in the local script.
local partButton = script.Parent
local module = require(game:GetService("ReplicatedStorage"):WaitForChild("Module"))
partButton.MouseButton1Click:Connect(function()
module:CreatePart()
end)
So, does something look familiar? Well, as you can see this would be the same way you would call
a regular function to trigger except this time you are calling it from the module script.
You can see this when in the module script we wrote it as:
function module:CreatePart()
ex of a regular/local script:
~~ function CreatePart()
Now, in the local script we are calling the function to happen:
module:CreatePart()
ex of a regular/local script:
~~CreatePart()
So, now when you click the button a part should apprear in your workspace.
How does this help?
Well this is much easier to call for 1 script instead of making multiple scripts for each player.
It also looks cleaner and cooler :)
There are many different uses for this and if you keep experimenting and practicing you will find
many more!