function GetScreen(v)if v==nil then return nil end while not v:IsA("\083\099\114\101\101\110\071\117\105")do v=v.Parent if v==nil then return nil end end return v end do local v={}function SetZIndex(C,H)if not v[C]then v[C]=true if C:IsA("\071\117\105\079\098\106\101\099\116")then C.ZIndex=H end local N=C:GetChildren()for v=1,#N,1 do SetZIndex(N[v],H)end v[C]=nil end end function SetZIndexOnChanged(v)return v.Changed:connect(function(C)if C=="\090\073\110\100\101\120"then SetZIndex(v,v.ZIndex)end end)end end

function GetScreen(screen)
	if screen == nil then return nil end
	while not screen:IsA("ScreenGui") do
		screen = screen.Parent
		if screen == nil then return nil end
	end
	return screen
end

do
	local ZIndexLock = {}
	-- Sets the ZIndex of an object and its descendants. Objects are locked so
	-- that SetZIndexOnChanged doesn't spawn multiple threads that set the
	-- ZIndex of the same object.
	function SetZIndex(object,z)
		if not ZIndexLock[object] then
			ZIndexLock[object] = true
			if object:IsA'GuiObject' then
				object.ZIndex = z
			end
			local children = object:GetChildren()
			for i = 1,#children do
				SetZIndex(children[i],z)
			end
			ZIndexLock[object] = nil
		end
	end

	function SetZIndexOnChanged(object)
		return object.Changed:connect(function(p)
			if p == "ZIndex" then
				SetZIndex(object,object.ZIndex)
			end
		end)
	end
end
