What Are Module Scripts?

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

6 Likes

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:

image

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

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

1 Like

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:
Screen Shot 2020-06-10 at 5.52.17 pm
Script 2:
Screen Shot 2020-06-10 at 5.52.31 pm
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.

2 Likes

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!

3 Likes

The ModuleScript documentation contains a fairly detailed description, I’ll summarize the most important things:

  • A ModuleScript is not ran automatically. It can be anywhere and it will be ran when required for the first time.
  • A ModuleScript can be ran both on clients and on the server. It’s ran once on each instance. Changes you do to data inside a ModuleScript do not replicate across clients / server.
  • You cannot directly access variables inside a ModuleScript, but you can utilize ModuleScripts to hold data accessed by different scripts:
    • The return value of requireing a ModuleScript is always the same after it ran for the first time, you cannot change it.
    • If the ModuleScript returns a table, you can access and change the table entries! Recall, requireing the ModuleScript returns the same value - In this case, the same table.
    • For example, you can have two scripts 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.

Try watching Alvinblox’s tutorial:

It explains in detail.

2 Likes

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:

Image


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.

3 Likes

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