function Maid.__newindex(X,f,V)if Maid[f]~=nil then error(("\'\037\115\' \105\115 \114\101\115\101\114\118\101\100"):format(tostring(f)),2)end local n=X._tasks local K=n[f]if K==V then return end n[f]=V if K then if type(K)=="\102\117\110\099\116\105\111\110"then K()elseif typeof(K)=="\082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110"then K:Disconnect()elseif K.Destroy then K:Destroy()end end end
--- Add a task to clean up. Tasks given to a maid will be cleaned when
--  maid[index] is set to a different value.
-- @usage
-- Maid[key] = (function)         Adds a task to perform
-- Maid[key] = (event connection) Manages an event connection
-- Maid[key] = (Maid)             Maids can act as an event connection, allowing a Maid to have other maids to clean up.
-- Maid[key] = (Object)           Maids can cleanup objects with a `Destroy` method
-- Maid[key] = nil                Removes a named task. If the task is an event, it is disconnected. If it is an object,
--                                it is destroyed.
function Maid:__newindex(index, newTask)
	if Maid[index] ~= nil then
		error(("'%s' is reserved"):format(tostring(index)), 2)
	end

	local tasks = self._tasks
	local oldTask = tasks[index]

	if oldTask == newTask then
		return
	end

	tasks[index] = newTask

	if oldTask then
		if type(oldTask) == "function" then
			oldTask()
		elseif typeof(oldTask) == "RBXScriptConnection" then
			oldTask:Disconnect()
		elseif oldTask.Destroy then
			oldTask:Destroy()
		end
	end
end
