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!

90 Likes

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.

8 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.

5 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.

10 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)

5 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.

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