I am new to lua. I know what scripts and local scripts do, but what does a module script do?
A brief explanation will help more than a couple words.
edit: I tried looking on google and couldnt understand it. So please do not ask me to go on google
I am new to lua. I know what scripts and local scripts do, but what does a module script do?
A brief explanation will help more than a couple words.
edit: I tried looking on google and couldnt understand it. So please do not ask me to go on google
ModuleScripts give you the ability to write reusable code libraries. They generally return a table of functions but don’t have to. You can get the return value of the module script by calling require
on the module.
Say you had a ModuleScript in ServerScriptService named MathExtensions:
The code in that module could look like
local math_ext = { }
function math_ext.sec(n)
return 1/math.cos(n)
end
function math_ext.cosec(n)
return 1/math.sin(n)
end
return math_ext
You can require it in a separate script.
-- ServerScriptService.Main [Script]
local math_ext = require(game:GetService("ServerScriptService").MathExtensions)
print(math_ext.sec(5)) -- 3.52532009
People were saying variables in a module script can be accessed by other scripts. Is this true?
Not exactly sure what you mean? Many use modules so they can share a table/function/etc across scripts.
Also I have one quick question, is there a way to use a variable in one script in another?:
Script 1:
local test = 100
script 2:
I can just say test without having to make a new variable
No it is not. What is your use case though? Why not return a table?
return {
test = 100
}
You can use them to write reusable code libraries as others said here. But, you can also use them to update the source of a public model if you have one.
Let’s say you have a fire alarm system and want it to be updated, then you only would have to update the module, your users don’t have to update anything because the fire alarm system requires() the code from the module.
I don’t know if you want to use modules for this type of thing, but if you want i can give more explanation.
Well If I need 2 scripts I dont want to re-make all the variables. It will be easier to make somethin in script 2 for instance
Then have your module return a table you can modify
That did not work, what I mean is if I type it the preview comes up like this:
script 1:
Script 2:
It does not recognise the variable.
Of course not. In script 2 you must require it.
-- Module
return {
test = 100
}
-- Script 2
local vars = require(module)
vars.test = 101
Very confused… Module underlines blue…
sigh
That was an example code. You need to fill out the path yourself.
I am not that good at scripting so i have no idea what your talking about…
Let’s say you have a big Table or a Function. I would not store that Table/Function in the Script to keep the Script clean and easy to read. Say for example you create a table in the Module script:
local prices = {
["Dog"] = 5,
["Wolf"] = 10,
["Dragon"] = 15
}
return prices
To get that table from a script you can easily do:
local petPrices = require(game.ServerScriptService.ModuleScript)
print(petPrices["Dog"])
print(petPrices["Wolf"])
print(petPrices["Dragon"])
This output for this would simply be:
5
10
15
You can also store functions in Module scripts. To create a function you use this format:
modulename.functionname = function(argumentsifneeded)
function here
end
So in our example you can do this in the module script:
prices.myFunction = function(printPrice)
print(printPrice)
end
So then in our server script we can then do:
local petPrices = require(game.ServerScriptService.ModuleScript)
petPrices.myFunction("Dog") -- Will print Dog.
petPrices.myFunction(petPrices["Dog"]) -- Will print 5.
Another thing you need to know about module scripts is that depending on where it’s placed, you can either require it from the client (local script) or the server (script). For example if you placed your Module script in ReplicatedStorage then you’d be able to require it from both the server and the client (assuming you’re familiar with how each service replicates its children). Say, however, that you place the module script in ServerScriptService, in that case you’d only be able to require it from the server.
Hope this helps!
The ModuleScript documentation contains a fairly detailed description, I’ll summarize the most important things:
require
d for the first time.require
ing a ModuleScript is always the same after it ran for the first time, you cannot change it.require
ing the ModuleScript returns the same value - In this case, the same table.require
a ModuleScript which returns a table. If you set a value inside the table on one script, the other script can read the changed value.If you’re unfamiliar with tables, I also recommend reading the tables documentation - Especially the last section, Tables as References, plays a big role here.
ModuleScrits are containers where code is stored until it is required by either a script or a LocalScript. What makes ModuleScripts so useful is that you can reuse the code that is in a ModuleScript over and over again in as many scripts as you want. If you define a function in a ModuleScript you can use that function multiple times in many other scripts. What makes this so useful is that you only need to define that function once and able to use it in multiple scripts, this is following the DRY(Don’t repeat yourself) principle.
In practice a ModuleScript could look like this:
local module = {} -- Creates a table named module
module.variable = true -- Creates a variable called variable and adds it to the module table
function module.foo() -- Creates a function called foo and adds it to the module table
print("bar")
end
return module -- Returns the module table.
Script:
local ModuleScript = require(script.Parent.ModuleScript) -- Requires "ModuleScript" placed directly under ServerScriptService
ModuleScript.foo() -- Runs the function "foo" in the ModuleScript and prints "bar"
print(ModuleScript.variable) -- Prints true
Hierarchy:
If you are more of a visual learner I would suggest you watch the YouTube video @TheDemoDeveloper linked as it should go over the basic principles of ModuleScripts.
If you want global variables you can used
Shared.Variable = variable
Or
_G.Variable = variable
And @Alvin_Blox has a great tutorial on how to use module scripts here
_G
or shared
isn’t related (though in any case you probably shouldn’t and instead store you data in a ModuleScript).