Hello everyone! It’s my second tutorial on devforum and it can be incompleted and/or unclear. Write in comments questions and I or someone answer.
1 - What is a Module and How it is used
Module is a ‘script with functions’ and it necessary for simplification of other scripts. Example: In 5 scripts you need to use one function, but you don’t want copy and paste it. You create a Module Script and write it. Later, you call this Module and write simple line (it at the bottom)
2 - Tutorial
Create a module script:
- Click on “+” button in module script’s parent (most in Replicated Storage for use on local and on server)
- Select “ModuleScript”
- Name it
Tune Module Script
After you create module script, lets tune it for you
-
After creating module script it have 3 lines —
Optional – rename the “module” variant to whatever you want (e.g. CreatePartModule) -
Create a function
Between first and last line write:
function YourModuleName.FunctionName(..., ...)
instead “YourModuleName” write your module name (which you did in step 1)
instead “FunctionName” write your function name (e.g. CreatePart)
instead dots write necessary variants (e.g. PartName or PartColor)
P.S. there can be any number of variants and functions
In next lines write, what should functions do
Don’t forget write “end” at end of function
Copy it
local module = {}
function module.FunctionName(first, second)
print("OK")
end
return module
3 - Using a Functions in other scripts
- For it in script (not module script) create variant:
local Module = require(game.ReplicatedStorage.MyModule)
Instead “game.ReplicatedStorage.MyModule” write your path to your module
P.S. Variant can be named as you wish
- Using a Function of Module
For use it write line where you want to run this function
Module.YourFunc(..., ...)
Instead “Module” write your variant of module
Instead “YourFunc” write need function of module
it looks like this:
Copy it
local module = require(game.ReplicatedStorage.MyModule)
wait(5)
module.FunctionName("first", 2)
3 - Practice
Lets try make your own module script. Use information from Tutorial
Result
Your module script look like this?
local Module = {}
function Module.CreatePart(PartName, PartColor, PartTransparency)
local Part = instance.new("Part")
Part.Name = PartName
Part.BrickColor3 = PartColor
Part.Transparency = PartTransparency
end
return Module
Your script look like this?
local module = require(g)
wait(10)
module.CreatePart("MyPart", Color3.FromRGB(100,100,100), 0.5)
If you answer “Yes!” you made correct module script and script!
Ending
Now you know, what Module is and how use it!
Hope, this tutorial been helpful for you.
Goodbye!