Currently, I am starting to include maids in my scripts – where ever needed. I have been looking at a few posts regarding the use of maids and none of them really gave a realistic sample of what it would look like in code. P.S this is not my first time using maids, I am just curious whether I have been doing it right this whole time, or whether I could fix/improve my methods.
Currently my server sided round system script (simplified) looks like the following
-- Services
-- replicatedStorage
--Serverstorage
-- References
-- Modules
-- maid
-- other modules
local Maid = MaidModule.new()
-- FUNCTIONS
-- blah blah
---------------
local InGameConnnection = workspace.Settings.Others.InGame.Changed:Connect(function(Value)
if Value then
-- Is now InGame
-- Set inGame connections/events
StartNight()
else
-- Disconnect ingame related connections and events!
Maid:DoCleaning()
-- reset values
end
end)
I suppose my question is should maid class be indexed on the outermost scope, or should it be encapsulated within a function (like I have seen frequently on OOP programming).
Like this *in comparison to the example above
local InGameConnnection = workspace.Settings.Others.InGame.Changed:Connect(function(Value)
if Value then
-- Is now InGame
local Maid = MaidModule.new() -- set the maid within the function scope instead of outside
-- Set inGame connections/events
StartNight()
else
-- Disconnect ingame related connections and events!
Maid:Destroy()
-- reset values
end
end)
Which of the two methods above would be preferable?
It would be greatly appreciated for some feedback and guidance regarding the use of Maids.
Thank you!
The Maid class itself can be defined at the top of the script like any other dependency.
Using it with OOP you would typically create a Maid as a member of an object you are creating using Maid.new()
example
local Maid = require(game.ReplicatedStorage.Maid)
local Object = {}
Object.__index = Object
function Object.new()
local self = setmetatable({},Object)
self_maid = Maid.new()
return end
end
function Object:Destroy()
self._maid:Destroy()
end
you can add things related to the object created to the maid and then when you destroy the object it will all get cleaned up nicely by just doing maid:Destroy()
2 Likes
Well noted! so just to confirm defining the maid class at the top will not change or hinder anything when not using OOP. and the example I used above is appropriate?
Correct, the example you showed could be a use of Maid.
Maid doesn’t have specific guidelines, essentially it does one core function - cleanup stuff.
It’s best used in the context of an object or a game mode or some aspect of your game that may have many variables, connections, parts, and even other objects that you will want to clean up when that context is no longer needed.
An example can be if you have a game mode and when the round starts you put a ball on the map, connect input event connections, and add some round specific UI. All of which you will want to be destroyed at the end of the round. So instead of manually remembering to destroy each thing (and if you forget you can cause memory leaks), you just throw it all in a maid and destroy the maid at the end of the round.
You can use Maids on the server and client. Of course you can’t have a single maid on both the client and server because they are different machines but you can make a separate maid for the client and when the server maid gets destroyed you can fire a RemoteEvent and tell the client to destroy it’s maid.
maid:GiveTask()
accepts the following objects
-
function - function will run when maid is destroyed
-
RBXScriptSignal - Roblox events will be disconnected when the maid is destroyed
-
Any table with a Destroy Method - For tables with a member called “Destroy” in either the table itself or the tables metatable, the :Destroy()
method will be called when the maid is destroyed. This means any Lua Object (OOP) can easily be cleaned up including custom signals from the popular Signal class and even other Maids.
-
Roblox Instances - Instance:Destroy() will be called on any Roblox Instance when the maid is destroyed.
There are also other features like being able to overwrite specific keys (objects) you put in a maid without destroying the entire maid. Overwriting a key in the maid will destroy the existing object and put a new object/nil in it’s place.
Here is the full article by Quenty, creator of Maid.
1 Like