--[=[
	@param instance Instance
	@return RBXScriptConnection
	Attaches the Janitor to a Roblox instance. Once this
	instance is removed from the game (parent or ancestor's
	parent set to `nil`), the Janitor will automatically
	clean up.

	:::caution
	Will throw an error if `instance` is not a descendant
	of the game hierarchy.
	:::
]=]

function Janitor:AttachToInstance(instance: Instance)
	assert(instance:IsDescendantOf(game), "Instance is not a descendant of the game hierarchy")
	return self:Connect(instance.AncestryChanged, function(_child, parent)
		if not parent then
			self:Destroy()
		end
	end)
end

