function Maid.DoCleaning(w)local z=w._tasks for w,y in pairs(z)do if typeof(y)=="\082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110"then z[w]=nil y:Disconnect()end end local y,o=next(z)while o~=nil do z[y]=nil if type(o)=="\102\117\110\099\116\105\111\110"then o()elseif type(o)=="\116\104\114\101\097\100"then local w if coroutine.running()~=o then w=pcall(function()task.cancel(o)end)end if not w then local w=o task.defer(function()task.cancel(w)end)end elseif typeof(o)=="\082\066\088\083\099\114\105\112\116\067\111\110\110\101\099\116\105\111\110"then o:Disconnect()elseif o.Destroy then o:Destroy()end y,o=next(z)end end
--[=[
	Cleans up all tasks and removes them as entries from the Maid.

	:::note
	Signals that are already connected are always disconnected first. After that
	any signals added during a cleaning phase will be disconnected at random times.
	:::

	:::tip
	DoCleaning() may be recursively invoked. This allows the you to ensure that
	tasks or other tasks. Each task will be executed once.

	However, adding tasks while cleaning is not generally a good idea, as if you add a
	function that adds itself, this will loop indefinitely.
	:::
]=]
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
