function GetScreen(a)if a==nil then return nil end while not a:IsA("\083\099\114\101\101\110\071\117\105")do a=a.Parent if a==nil then return nil end end return a end do local a={}function SetZIndex(R,l)if not a[R]then a[R]=true if R:IsA("\071\117\105\079\098\106\101\099\116")then R.ZIndex=l end local g=R:GetChildren()for a=1,#g,1 do SetZIndex(g[a],l)end a[R]=nil end end function SetZIndexOnChanged(a)return a.Changed:connect(function(R)if R=="\090\073\110\100\101\120"then SetZIndex(a,a.ZIndex)end end)end end
-- returns the ascendant ScreenGui of an object
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
