So, I was just clicking random buttons and then I found module scripts. Can anyone explain to me what they are and what do we use them for?
A module script is a script that contains functions or variables that you can use in multiple scripts, by requiring them. For example:
module:
local module = {} ---------module can be anything, but most of the time it is a table function module.printsomething(A) print(A) print("Module script!") end return module ---------basically the table gets returned when you require() this module
script:
local module = require(workspace.Module) -----or wherever your module is located module.printsomething("hello world!")
The script outputs:
hello world!
Module script!
A Module Script
is basically a replacement for the Global Functions which for some reason isn't deprecated yet... But they basically allow you to run the same bit of code across multiple scripts / local scripts / modules.
To reference one...
local ModuleScript = require(game.ServerScriptService.ModuleScript)
Module scripts are almost like classes meaning that you can have variables and functions inside them...
local module = {} module.variable = 5 module.doesNothing = function(val) val = val - val + val return val end return module
You can call the functions and variables like so...
local ModuleScript = require(game.ServerScriptService.ModuleScript) print(ModuleScript.doesNothing(10)) print(ModuleScript.variable)
Note, a module script does not run ANY code until it is required. This means that you can't have events such as... game.Players.PlayerAdded:Connect(function(player) ... end)
Good uses for them include the ability to run the same code from anywhere on the Module Script's respective side (server or client). The ability to house libraries and data to reference from anywhere.
Example
I have a lot of tools in my game, but I don't want to make a code block for each of the tools to equip it. So I make a Module Script that I can pass the tool info to, and it positions and equips it on my player.
Helpful Link: https://wiki.roblox.com/index.php?title=API:Class/ModuleScript