I’ve seen many youtubers making games in roblox and what I noticed is that most of them used
Module Scripts
. What do you mainly put inside a Module Scripts
? Does it work like other scripts? Please help me on this topic. Thanks!
Module scripts are used to organize related code.. Think of it like roblox services.. Services like Players
, ReplicatedStorage
, etc are all in an organized form.. this way so you don't have to do something like game.ServerStorage:GetPlayers()
- clearly this doesn't make sense. So things related to players are organized into the Players
service..
imagine your working on a pet system.. you wouldn't want code like the following to be what gives you access to the player's Pets.. Because PortalSystem
has nothing to do with PetSystem
local PortalSystem = require(game ... PortalSystem); local pets = PortalSystem:GetPlayerPets(player)
in other words, have PortalSystem
handle portal related code, and nothing else, and similarly to PetSystem
local PetSystem = require(game .. PetSystem); local pets = PetSystem:GetPlayerPets(player);
another advantage of module scripts is the fact that they can be used by other scripts..
imagine when a player joins the game, you don't want all the code handling pets to be in the same script handles the PlayerAdded
event.. you could just require the ModuleScript that has pet related things, and simply just call a function(or whatever) of the interface that the ModuleScript returns..
Now imagine you have all the code in the same script, then a player buys a new pet, all the code that handle transaction of the pet will have to be in that same script, because there's no other ways of accessing the functionality for the pet system.. if you just used a module script, then you could just require the pet system module from a script handling transactions, and therefore keep the code simple, and clean..
inside of a module script you can write functions you want to access from your regular code like say I wanted to kill a random player I would go to serverscriptservice and then I would go to a module script and do this
local module = {} function module.kill(player) Workspace.player.Humanoid.Health = 0 end return module
and in the regular script, I would say
local module = require(script.Parent:WaitForChild("ModuleScript"))- or whatever the name is local randomPlayer = game.Players:GetPlayers() x = [math.random(1,#game.Players:GetPlayers())] module.kill(x)
basically If you don't get it Module scripts are a way of organizing code, I could have written the kill function in my code but that wouldn't be efficient, that is why we use module scripts
modular code that you want to access/use from other scripts. You can also store values/tables in one like this:
local module = {} module.Things = { ['hey'] = 69, ['something'] = "lol" }
Hey Grazer022,
I hope this will be useful to know about. Module Scripts.
The Module scripts are very useful when you need to store code, and so you have code that's stored. I just want to say you're not alone asking this question. This question is quite frequent to ask. So Module scripts can be very useful if you're making e.g. a shop for pets in a simulator. Like let's say an example of if I'm making a cat pet for a simulator. Then I say ["Cat"] = {. That tells the pet. Then I want to tell the cost of the Cat. Then I say ["Cost"] = 100;. I need to end the cost with a semicolon. Then mabye I would like to have the health for my cat. Then I can say ["Health"] = 200;. And again, I need to end the number with a semicolon. Now I can show how it would look
`local module = {
["Cat"] = { ["Cost"] = 100; ["Health"] = 200;
};
}
return module`
I hope this helped you understand Module Scripts a little more!
Sincerely,
Ducksneedhelp - Contributor
`