When should I use a ModuleScript?

Hi guys. I’d like to know when to use a module script. Is it better to use module scripts in all cases, or just sometimes? If just sometimes, when should I use it?

3 Likes

Use it to store functions or variables that need to be accessed from multiple scripts.

(But if you want, you can use it even if a single script will be accessing it, just to keep things organized.)

8 Likes

What he said.

I use them when I want to create my own Class, something that needs to be accessed and used/created several times.

I have a little showcase where you can select characters to be on your team. The script which handles team selection needs to access each character to create the character selection list. The script which stores which characters I have needs access to my own copy of that character. The script which shows the characters I’ve selected will need access to those characters. Several scripts will need the stats of my characters.

I end up with a folder of ModuleScripts, each one for their own character:

image

Each script contains their stats and data:

My character selection interface can now loop through my character list and grab the names etc of each character to create the character list:

image

As you can see, I’ve added a function called ‘new.’ If someone selects a character, i call .new() on that character from another script and add the character to my team list.

I want to be able to actually turn into each character, attack with each character, and do several other things. So they all need attack() functions. Since they all need them, I should probably just make one copy of those functions. And put them in a ModuleScript!

image
CureAbstract contains functions every Cure/Character will need:

What I’ve really done is create something similar to a Vector3, CFrame, Part etc. Something you can create multiple instances of, which has their own convenient functions and individual properties.

You don’t need to understand how to create these just yet, it’s kinda complex ish. But this is the OOP/Object Oriented Programming construct. Not sure if you’re familiar with it, I wasn’t when I started using roblox and module scripts.

I find module scripts/OOP a very useful way of creating things you need lots of, like guns, characters, attacks, anything that needs lots of stats and operations. Or less tangible things like algorithms which need to be used by multiple scripts.

4 Likes

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