Before you understand require you need to understand ModuleScripts.
ModuleScripts are generally used to write reusable libraries of code that you can use across scripts. Instead of duplicating the same code across scripts, you can instead have it in one place, i.e. a module. Then all the scripts that need the same library can just require it.
There are two ways to require a ModuleScript.
First one, is by getting a reference to the actual ModuleScript instance.
local math_ext = { }
function math_ext.secant(n)
return 1/math.cos(n)
end
function math_ext.factorial(n)
return n == 0 and 1 or n*math_ext.factorial(n - 1)
end
return math_ext
Then you would require it in a different script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local math_ext = require(ReplicatedStorage.math_ext)
print(math_ext.factorial(5))
The second way is to require by its asset ID. You need to upload it to Roblox as a free model, but naming the ModuleScript instance "MainModule".
local MainModule = require(123456789)
And it works the same way.
3 comments
So you mean like that require gets the module script that in the Serverscriptservice?
A Module script can hold functions that local script and sever scripts can access by require(modulescript), example
As FirezDevv said, Modulescript can hold information accessible by the server and client.