How to use module scripts!

This tutorial will cover how to use module scripts!

this is my first tutorial here, so let me know if I’ve done something wrong.

So what are module scripts you may ask?

well, module scripts contain functions that can be accessed from any script in your game as long as you use require(location of script). this can be very useful for making Framework for your game! More info on module scripts

How do we use them?

So lets get started making our first module script!

first, go to serverscriptService and insert a module script.

then after you insert it, your screen should look like this -

now i know this looks confusing. one question you might be asking is, why does this script not start with print("Hello World!")? Well we dont really need to know this right now. so im going to continue.

we can now make our first function!

local module = {}

	function module.myFunction()
	
	end

return module

so, where it says module.myFunction(), that basically adds a function to the module (kinda obvious lol)

now we put our code inside this function. im going to start off with a simple function to add some numbers and multiply some numbers.

local module = {}


function module.myFunction(Multiply1,Multiply2,Add1,Add2)
	local number = 0
	number = Multiply1 + Multiply1 * 2
	number = Multiply2 + Multiply2 * 3
	
	number =  Add1 + Add2
	
		return number
	end


return module

so now this is my function. (yours can do anything).

Now (Last part) insert a script into server script service

and do this

local myFirstModule = require(script.Parent.ModuleScript) -- replace module script with your modules name

-- the function "require" just gets your module so you can use its functions


local number = myFirstModule.myFunction(2,3,5,10) -- the numbers im adding and multipying

print(number)

-- and here im calling the function

now in the output you should see something like this -

i hope this helped somebody!

please let me know if i did something wrong because this is my first tutorial!

4 Likes

Nice tutorial, but there are some things that I think are important for understanding what module scripts really are that you didn’t cover

Understanding that module scripts can return anything (any type, they can’t return nil or more than 1 value) can clear up the fact that a module script really are just tables, (most of the time)

But you can also make a module script return a function directly, which can be useful in some cases:

-- // Module script, lets name it TestModule

-- A function is returned rather than a table containing functions and stuff
return function()
	print("Hello world")
end

-- // Normal script

local Module = require(script.TestModule)
print(type(Module)) --> function (while for a "normal" module, this would be a table)
Module() --> "Hello world"

There is also the caching behaviour that is often confusing beginner scripters. It is fairly common to see a question regarding module caching in help discord servers or on the forum

It is also a bit tricky to explain

If not using parallel lua:
– Modules run only once (the first time they are requires), and their result (the table) is cached for later requires.
(But the client and server are separated, so it runs once on the client, and once on the server)

– When a script modifies the returned table, since tables are stored by reference (aka you get where the table is stored in memory rather than getting a “raw” table), if one script modifies this table, other scripts that required the module will see the modified table

If using parallel lua:
– Every actor acts as a separate entity, so scripts associated with 1 actor wont get the cached result of scripts associated with another actor. This means you cannot have shared data in a module accessible from 1 actor and another actor, just like you can’t shared data in a module accessible from the client and the server

4 Likes

Thanks for the reply!

there is a lot i didn’t cover like you said, but i decided to keep this mostly simple.

when i tried to learn modules from youtube, and dev forum posts a while back, i was just getting constant information that i didn’t know what to do with…

so i thought the best approach would be to just keep it simple, and clear.

i did also link the documentation

here incase they wanted to learn more.

3 Likes

Seeing roblox devs who still use light mode but know who to use a module script incounting value 2, found.

1 Like

i used to use dark, but started a new roblox account and light mode just looks so good!

edit: and also only 2 people is crazy…

1 Like