Questions about Module scripts

So lately I have started working on code organization and formatting and one thing I have learned about is module scripts. I understand their purpose but I have 3 questions about it

  1. I’ve seen multiple people say that you can store functions in it but why not just use collection service?
  2. I have also heard the whole storing variables in module scripts but once again why not just include it in normal scripts
  3. could anyone give me an example of when they would be used?
2 Likes

They might be used when you want to have shorter code on one script. Instead of having all the code on one script, you could have a ModuleScript as a child with some of the code needed, and then call upon the ModuleScript when that specific function is needed. It can make navigating a script a lot easier.

Think of a ModuleScript as a library, with each function inside it as a book. When a script needs to perform a specific function, instead of having to copy and paste the same code over and over again, you could just call upon the function in the ModuleScript and then the function will perform just as good. This works across multiple scripts, too, so it cuts down even more on the amount of coding needed.

You can also use the functions from all you scripts and not just one script.

Imagine you have a module that creates a notification for the player, you can know make a notification from any of your scripts without having to copy/paste the function everywhere

Another exemple :
You can make a module that only stores values like all your game properties, and then use the values from any script.

1 Like

Thanks this actually makes me realize that a lot of problems I’ve had with scripts in the past could have been solved with a module script

Very good definition thank you

1 Like

I don’t believe this is the right forum, but I’ll answer anyways.

  1. CollectionService is used for Instances. If you meant class (OOP) vs CollectionService, then it is completely up to you which one you prefer to use. For simpler things, CollectionService is very powerful. For more complex objects that has player inputs, for example, classes are your way to go.

  2. You can, however, it is very useful to have a centralized module for your variables if you plan on reusing them. For example, it is very nice to have a Constants ModuleScript containing all the information the server and client can access. It is very efficient changing one line of code vs multiple scripts. It is also useful as an organization method if you wish to do so.

  3. Shared functions, classes, server-client information sync, etc. It has many uses. Here are a few examples on how I use ModuleScripts:
    A. Shared functions - For my games with experience data, I don’t bother keeping track of the level, just the total experience. This means I have a ModuleScript that does these level and experience calculations the server can use to manipulate data and the client to display the converted data. Another example is I have extra table and math functions I created that I can just require and use.
    B. Classes - With OOP, you can essentially design your own game objects with it’s own properties and functions. Having them separated is good for organization, and most importantly, re-usability.
    C. Relating to point A, I usually have a Constants ModuleScript that contains a lot of different types of information. I use it to store contentId (or game assets, like audio and images) since I script my Guis. I may also add extra information that the server can use as well, such as text tags (ex: TAG_CURRENCY with the value Gold) so I can easily swap out all in-game currency text by changing that single value.

It can also be used to separate your framework if it’s too large. You can have ModuleScripts in a single directory handling different types of things. You can argue that you can use normal (Local)Scripts for that, however, I add extra functions to my ModuleScripts in case I want to read or write data at any time.

Thank you for your explanation your examples are very useful.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.