Modules - What are they and how to use them? [CHANGED]

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:

  1. Click on “+” button in module script’s parent (most in Replicated Storage for use on local and on server)
    Снимок экрана 2022-01-21 205041
  1. Select “ModuleScript”
    Снимок экрана 2022-01-21 205059
  1. Name it
    Снимок экрана 2022-01-21 205117

Tune Module Script
After you create module script, lets tune it for you

  1. After creating module script it have 3 lines —
    Снимок экрана 2022-01-21 162313
    Optional – rename the “module” variant to whatever you want (e.g. CreatePartModule)

  2. 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

  1. 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

  1. 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!

11 Likes

Nice tutorial but i am not getting it.

Edit: I mean i did learn module script from other players, but i do not understand anything from this tutorial.

I make it easier and clearer

It’s really “easier and clearer”?

Thats pretty cool! But i think u missed the backticks in the “copy it”.

But ima try that thing out.

1 Like

One thing I would like to point out is that the variable in the ModuleScript doesn’t have to be a table. You can assign strings, numbers, etc. If you are only using the ModuleScript for one variable inside the table. You can just do something like this.

local Message = "Test Message"

return Message

Then you can do whatever you want with the variable after you require it.

1 Like

I am pretty sure that even this could be shortened to

return DATATYPE

So in this case it would be

return "Test Message"

But i thought return only works from a function, are you saying it can be used anywhere?

I’ve never ran into an issue where return didn’t work because of the position.

Return can be called anywhere but it will prevent anything from running in the current scope, just like break which also exits the entire loop it is called in.

I didn’t know you could even just do a return with a value and not just a variable. That’s interesting how that works.