How do you use Modules to control your whole game

Hello!

I understand what modules do and are but I don’t understand how you could control a whole game with one as some people say. Because like how do you do a shop module, or like how do you make a module to control a loading screen. I just don’t understand how this can be done and how much better it is then just writing multiple scripts in all the different services and stuff.

Thanks for reading and even helping me understand. Also, if this is in the wrong place, just tell me as I am unsure. :grinning:

2 Likes

This would probably be more suitable in Scripting Support.

As for your question, I imagine people can use purely ModuleScripts to control their games by having a Script (or LocalScript if it’s for the client) initialise a whole amount of the necessary modules. Otherwise, they just require the ModuleScripts where necessary, keeping them as classes/objects almost. It removes the need to repeat yourself across multiple Scripts/LocalScripts when you can just grab the code from creating it as a function in a module.

1 Like

But I just don’t get how so much can be done by one library, because a whole game is usually a ton of stuff.

If a whole game is controlled by only one module, I’d say they nest all their functions in that sole module and centralise it, while getting other scripts to require said module. I’m unsure to how effective/efficient this is, so I can’t say if I’d recommend this method. Someone else here can vouch its reliability.

You should learn about OOP(Object Oriented Programming) makes things a lot easier for people.

1 Like

I have learned about it, I don’t know how people make entire games with modules.

2 Likes

Mostly I use modules for functions and values (configurations) that I will use in other scripts, and I also use them to keep my code as clean as possible.

1 Like

I am starting to understand, your suffix command would allow you to make any number shortened with 1 line instead of it taking 14 lines for each one so that could be one thing that can be used for your entire game!

Here’s a snapshot from a game I’m working on:

Our ModuleScripts are handled by a simple module provider I wrote (known in our team as MP), which manages loading and distributing our modules. It may sound redundant to have your code split up into hundreds of ModuleScripts, but with some good organization, we’re able to easily manage the game by editing its respective module group.

7 Likes

If the code is going to be used multiple times in more than one script instance, then use module. A great example would be UI components, API wrappers, etc.

The use of module can ease development, especially when you want to make changes to something that is being used multiple times.

Edit: For me, I have a small loader which loads modules (for my UI framework)

return setmetatable({},{
    __index = function(self,key)
        return require(script[key])
    end
})

Which the method to get a certain module will be:

Loader.ModuleName
2 Likes

Good job on organization and I think I finally understand how it is possible to control your entire game just through modules!

Thank you everyone for explaining this and helping me get this through my thick skull, thank you! :laughing:

In case you require more reading, you can search up “single-script architecture”. There’s a couple of threads with some information and resources to help you get used to the workflow.

https://devforum.roblox.com/search?q=single-script%20architecture%20%23help-and-feedback%3Ascripting-support%20

PS: Code Review is for requesting a review for code that already works in the realm of improvements. I have recategorised this to Scripting Support, which you can use to ask your questions regarding programming. Please read category guidelines before posting.

2 Likes