How to use a Maid class on Roblox to manage state

The Maid class on Roblox is a useful class that I use to help manage state, especially in regards to signals or events. Be warned: This is a fairly technical article!

I use Maid extensively in Jailbreak. It vastly simplifies the cleaning up of events/objects, and gives me confidence that I will avoid memory leaks.
~ @badcc, developer of Jailbreak

Read it here:

You can download the Maid source code here:

Let me know if you have any questions about how to use maids, or what good patterns are in them!

145 Likes
Dice Manager | Handle Connections, Custom Wrap Calls, & Task Schedules
Is there a way to destroy objects of a class using metatables?
Can I avoid overriding inherited methods with OOP
Unable to break loop + Touched Event isn't working
Unsure how to assign collision groups through teams
How can I prevent memory leaks in my game?
High Memory Usage
Performance considerations, improvements, and optimizations when making a game
Memory leak - high usage in physics/broadphase
Streaming part problem with Infinite Obby Generated
Any improvements that should be made to this R6 foot plant module?
Too much NPCs for a for loop to handle
How to know if my have a memory leak
Scripting Resources Mega List
Beams for 2d objects?
Quenty's Maid Module but with intellisense
Quenty's Maid Module but with intellisense
How to optimized my game abilities?
Tweening module having memory leak issues
Why you should use Wally, a package manager for Roblox
Help with memory leaks
Is it possible to override a connected RBXScriptSignal?
Trying to find the best practice in handling a connection using oop. does anyone have suggestions?
Removing a wait(), functions are called in the wrong order. Is the ModuleScript implemented correctly?
Events/Connections from previous playsessions not disconnecting
Populus Romanus Credits
NumberAnimation | Animate numbers with ease!
How would a variable behave if it equated from one function to another?
I don't get why I should use Collection Service
How do I get rid of runService.Heartbeat in OOP

Bless Maids. They’re so helpful in my upcoming RPG where there’s a lot of events tied to animations and time-sensitive things. It’d probably be leaking memory out the sides if not for Maids keeping everything nice and tidy.

13 Likes

The article imo was more focused on the API and usefulness of maids than addressing the actual problem it solves.

Giving some code and then explaining how it might cause issues doesn’t give a very in-depth analysis on what the code is doing and why there is an issue.

6 Likes

Yeah. I’m not super satisfied with the overall article quality. I might do a few case studies to demonstrate how useful it is as a pattern.

The problem is for beginner programmers Maids aren’t that great. It’s only as you scale up in advance usage that Maids become more and more useful. They have abstract away a lot of complexity that beginner programmers don’t tend to have, and create impact mostly in areas that have long-term consequences, not short term ones.

12 Likes

Make sure you name things appropriately rather than something arbitrary like “GivePromise”, especially when designating said system for others to use, as no one can read your mind to find out what that means.

2 Likes

Maids are such an elegant and simple system with lots of versatility. I wish I had learned about this earlier. It’s pretty fun learning the different tricks they can pull off in your code.

Thank you for sharing this with the community.

(Sorry for bumping this 2 years later. I think more developers should see this and I wanted to share my appreciation)

6 Likes

Seems like an overkill solution

1 Like

Wouldn’t you want to try to put all of your code in the least number of scripts? I don’t think it would get to that point of only having one connection if you combine code.

1 Like

Yes broadly speaking you probably want to use the least amount of Scripts (not Modules).
However I see a lot of people using a single Script (per Server/Client) pattern where those scripts require possibly hundreds of modules.

The point is to make a big code base maintainable because putting everything into a single script becomes pretty much the opposite of that.

Having said that you would likely encounter many modules that have connections and here I feel like using a Maid for every case is overkill but certainly makes things look cleaner.

1 Like

Hmm. I don’t understand why you need many modules unless if you are using promises or datstore2 or something like that. You can coroutine.wrap needed loops and use CollectionService for object appliance.

Because you are going to end up with a big hard to read/maintain mess if you put everything into one script.
It might not matter so much for a smaller project but even then it’s just good practice if you ever plan on expanding it.
Essentially you want to enapsulate behavior and make it more predictable and reusable.

3 Likes