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!