local M do local L={GiveTask=function(M,L)local o=#M.Tasks+1 M.Tasks[o]=L return o end;DoCleaning=function(M)local L=M.Tasks for M,o in pairs(L)do if type(o)=="\102\117\110\099\116\105\111\110"then o()else o:disconnect()end L[M]=nil end end}local o={__index=function(M,o)if L[o]then return L[o]else return M.Tasks[o]end end,__newindex=function(M,L,o)local F=M.Tasks if o==nil then if type(F[L])~="\102\117\110\099\116\105\111\110"and F[L]then F[L]:disconnect()end elseif F[L]then M[L]=nil end F[L]=o end}function M()return setmetatable({Tasks={},Instances={}},o)end end local function L(M)local L=M local o=Players:GetPlayerFromCharacter(L)while not o do if L.Parent then L=L.Parent o=Players:GetPlayerFromCharacter(L)else return nil end end return L,o end
--[[Maid
Manages the cleaning of events and other things.
 
API:
	HireMaid()                        Returns a new Maid object.
 
	Maid[key] = (function)            Adds a task to perform when cleaning up.
	Maid[key] = (event connection)    Manages an event connection. Anything that isn't a function is assumed to be this.
	Maid[key] = nil                   Removes a named task. If the task is an event, it is disconnected.
 
	Maid:GiveTask(task)               Same as above, but uses an incremented number as a key.
	Maid:DoCleaning()                 Disconnects all managed events and performs all clean-up tasks.
]]
local MakeMaid
do
	local index = {
		GiveTask = function(self, task)
			local n = #self.Tasks + 1
			self.Tasks[n] = task
			return n
		end,
		DoCleaning = function(self)
			local tasks = self.Tasks
			for name, task in pairs(tasks) do
				if type(task) == "function" then
					task()
				else
					task:disconnect()
				end
				tasks[name] = nil
			end
			-- self.Tasks = {}
		end,
	}

	local mt = {
		__index = function(self, k)
			if index[k] then
				return index[k]
			else
				return self.Tasks[k]
			end
		end,
		__newindex = function(self, k, v)
			local tasks = self.Tasks
			if v == nil then
				-- disconnect if the task is an event
				if type(tasks[k]) ~= "function" and tasks[k] then
					tasks[k]:disconnect()
				end
			elseif tasks[k] then
				-- clear previous task
				self[k] = nil
			end
			tasks[k] = v
		end,
	}

	function MakeMaid()
		return setmetatable({ Tasks = {}, Instances = {} }, mt)
	end
end

local function GetCharacter(Descendant)
	-- Returns the Player and Charater that a descendent is part of, if it is part of one.
	-- @param Descendant A child of the potential character.

	local Charater = Descendant
	local Player = Players:GetPlayerFromCharacter(Charater)

	while not Player do
		if Charater.Parent then
			Charater = Charater.Parent
			Player = Players:GetPlayerFromCharacter(Charater)
		else
			return nil
		end
	end

	-- Found the player, character must be true.
	return Charater, Player
end
