local I=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local x=require((I:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101"))local u=x:GetResource("\069\120\116\101\114\110\097\108\085\116\105\108\046\078\101\120\117\115\073\110\115\116\097\110\099\101\046\078\101\120\117\115\079\098\106\101\099\116")local U=u:Extend()U.DisplayOrders={}U:SetClassName("\068\105\115\112\108\097\121\079\114\100\101\114\067\111\110\116\114\111\108\108\101\114")function U.Register(u,I,x)x.Changed:Connect(function()if x.Value then local x=1 for I,u in pairs(u.DisplayOrders)do x=math.max(x,I.DisplayOrder)end local U=x+1 I.DisplayOrder=U u.DisplayOrders[I]=U else u.DisplayOrders[I]=nil end end)end return U
--[[
TheNexusAvenger

Ensures the latest open window has the highest display order.
Class is static (should not be created).
--]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))

local NexusObject = ReplicatedStorageProject:GetResource("ExternalUtil.NexusInstance.NexusObject")

local DisplayOrderController = NexusObject:Extend()
DisplayOrderController.DisplayOrders = {}
DisplayOrderController:SetClassName("DisplayOrderController")



--[[
Registers a frame to control.
--]]
function DisplayOrderController:Register(ScreenGui,OpenStateValue)
    --Connect the open state.
    OpenStateValue.Changed:Connect(function()
        if OpenStateValue.Value then
            --Get the highest display order.
            local HighestDisplayOrder = 1
            for OtherScreenGui,_ in pairs(self.DisplayOrders) do
                HighestDisplayOrder = math.max(HighestDisplayOrder,OtherScreenGui.DisplayOrder)
            end

            --Set and store the display order.
            local NewDisplayOrder = HighestDisplayOrder + 1
            ScreenGui.DisplayOrder = NewDisplayOrder
            self.DisplayOrders[ScreenGui] = NewDisplayOrder
        else
            --Remove the display order.
            self.DisplayOrders[ScreenGui] = nil
        end
    end)
end



return DisplayOrderController