Help with modules

Hello, I have a few questions about module scripts. I see a lot of games use tons of modules and only 1 or 2 server scripts, how do they achieve this and how would I get modules to work together? They can’t require each other, correct?
Also, what are some advantages module scripts have?

1 Like

Modules are extremely beneficial to organizing, creating references, and can even be good for performance in some cases. Modules can require each other, however they cannot require themselves.

The main use of modules is to reference functions and tables of information. Without modules, you would have to define your necessary functions and variables in every script that needs to use them. With modules, you can reference just a single module across multiple different server and client scripts and have all of them use the same functions. This really helps remove clutter and is especially easier to navigate rather than searching through extremely long and cluttered scripts for a specific function. You can even share the variables between them for a quick way to have server-client communication.

Modules have the ability to be required with a Marketplace link which also helps keep things less cluttered. Published modules are a great tool since you can share them with many people for many different use cases. Also by using a Marketplace link, the data from the module will always be up to date. Here’s an example of a published Marketplace module that can be used for different things.

You can read a quick introduction to ModuleScripts here.

2 Likes

Adding to what @HeyWhatsHisFace has said above,

Lets say you have multiple coins you want the players to get X amount of cash when collected.
A beginner would have created a script for the collecting part for each individual coin.The problem with that is, if you wanted to change something, or modify, you would need to do that for each of those scripts, if you had 100 coins, does it seem normal to you to modify now 100 scripts each by one?

There’s where a module script comes in.
It allows you to manage the coins collection in 1 function stored in a table, what you could do is, whenever a player touches a coin, you’d call that function from the module.
Now - if you’d need to modify something, you’d just go to that single function and modify it! MUCH BETTER! Isnt it?

[Although, you could achieve the same in 1 single normal script, but modules help you manage various functions and stuff together, instead of calling the same functions, you could require the module.]

Images of what I am talking about:

3 Likes

Okay thank you, I thought modules couldn’t require each other. This is very helpful now because I wanted to put my datastore into a module script, this would include the current datastore name as a single value and loading, saving and giving data. I also have a module script that all players data is loaded into, I would need to access that because saving it saves the data inside that module.
If that made sense, Data is loaded (if there is any) then is sent to a module script called PlayerData then a new table with the players name is created and their data is put into that module

Thank you too, though I would’ve probably made a folder with for them and just used i, v in pairs but then again that can easily be exploited if a exploiter sees a “Coins” folder

Also now that you edited it and put that image, do you mean storing the function and the coin inside the module? I don’t fully understand what you mean

If my studio has’t been crashing , I could’ve explained it better.

But overall,
You could store a function to collect coins in a module script, and when needed, require it.

Ohh alright, I think studio is having issues, I have a friend that hasn’t been able to get on it either.
I am not entirely new to modules, I have a little experience but I’ve never used them because I usually keep everything in 1 - 2 scripts but recently realized how messy and hard it is to find certain functions
And it’s even worse trying to fix bugs like that but this still helped

ModuleScripts can require other ModuleScripts but two ModuleScripts cannot require each other (recursive require issue).

Okay thank you, very helpful [limt]