Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
5

what are Module Scripts mainly used for?

Asked by
Grazer022 128
3 years ago

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!

0
There is a lot of uses of ModuleScript. The most basic uses of it is make your code reuse-able and easily to maintain it. Block_manvn 395 — 3y
0
For example, if you have a lot of "kill script" for every "kill parts" and the scripts have error in it, so you have to fix every single script in the parts(imagine how painful is it). The solution is using ModuleScript. By making one script that can use by every other script, you can easily edit and improve it in the next time. Block_manvn 395 — 3y

4 answers

Log in to vote
4
Answered by 3 years ago
Edited 3 years ago

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

0
Thanks! I understand much more now. I appreciate the help! Grazer022 128 — 3y
Ad
Log in to vote
2
Answered by 3 years ago

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

0
you have to require modulescripts or you cant use their code Hope this helps :) ayuu_ondev 60 — 3y
0
thanks for helping me! Grazer022 128 — 3y
Log in to vote
1
Answered by 3 years ago

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"
}
0
Thank you so much for your help! Grazer022 128 — 3y
Log in to vote
1
Answered by 3 years ago

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

`

0
thank you! I appreciate the help! Grazer022 128 — 3y

Answer this question