function Maid.DoCleaning(Q)local R=Q._tasks for Q,Y in pairs(R)do if typeof(Y)=="\082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110"then R[Q]=nil Y:Disconnect()end end local Y,n=next(R)while n~=nil do R[Y]=nil if type(n)=="\102\117\110\099\116\105\111\110"then n()elseif type(n)=="\116\104\114\101\097\100"then local Q if coroutine.running()~=n then Q=pcall(function()task.cancel(n)end)end if not Q then local Q=n task.defer(function()task.cancel(Q)end)end elseif typeof(n)=="\082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110"then n:Disconnect()elseif n.Destroy then n:Destroy()end Y,n=next(R)end end

function Maid:DoCleaning()
	local tasks = self._tasks

	-- Disconnect all events first as we know this is safe
	for index, job in pairs(tasks) do
		if typeof(job) == "RBXScriptConnection" then
			tasks[index] = nil
			job:Disconnect()
		end
	end

	-- Clear out tasks table completely, even if clean up tasks add more tasks to the maid
	local index, job = next(tasks)
	while job ~= nil do
		tasks[index] = nil
		if type(job) == "function" then
			job()
		elseif type(job) == "thread" then
			local cancelled
			if coroutine.running() ~= job then
				cancelled = pcall(function()
					task.cancel(job)
				end)
			end

			if not cancelled then
				local toCancel = job
				task.defer(function()
					task.cancel(toCancel)
				end)
			end
		elseif typeof(job) == "RBXScriptConnection" then
			job:Disconnect()
		elseif job.Destroy then
			job:Destroy()
		end
		index, job = next(tasks)
	end
end
