What's the best way to fix overlapping of guis?

So, I have multiple frames, and when we click an button the frame opens, but then, I have another button and another frame at the same position, when I click the other button, it puts up the frame on the same position of the other frame, I want to make it so like, when we open the other frame, the first one closes/tweens.

You could always just set the ZOffset of the frame you want to be infront

You could make a handler script or some sort that handles all the frame opening and closing like so:

local guis = {
    [script.Parent.Button1] = { -- Button
        Frame = script.Parent.Frame1, -- Path to frame
        Open = UDim2.new(0.5, 0, -0.5, 0), -- Open position of the frame
        Close = UDim2.new(0.5, 0, 0.5, 0), -- Closed position of the frame
        IsOpen = false -- Toggle variable
    },
    
    [script.Parent.Button2] = {
        Frame = script.Parent.Frame2,
        Open = UDim2.new(0.5, 0, -0.5, 0),
        Close = UDim2.new(0.5, 0, 0.5, 0),
        IsOpen = false
    },
}

for button, info in pairs(guis) do -- loop through all the buttons
   info.Frame.Position = info.IsOpen and info.Open or info.Close -- set the frame position corresponding to initial toggle
   
   button.MouseButton1Click:Connect(function()
       info.IsOpen = not info.IsOpen -- inverse the variable
       info.Frame:TweenPosition(info.IsOpen and info.Open or info.Close, nil, nil, 0.25, true) -- tween the position of the frame to the open position if toggle is true or close it
       
       for _,i in pairs(guis) do -- loop through all buttons again
           if not i.IsOpen or i == info then continue end -- if the frame is already closed or if the current iteration is the one we just opened/closed then skip the loop
           i.IsOpen = false -- close it
           i.Frame:TweenPosition(i.Close, nil, nil, 0.25, true) -- tween to closed position
        end
    end)
end

PS: I haven’t tested this in studio, this is just something I came up with.

But is there any much more simpler way to do it?

I have the same working workflow as you said, but i’m trying to make it easier for me to handle it

This is easy to handle in my opinion. I’m that sort of guy who works with modules and stuff that’s why I’m used to maintain these sort of code lol.

Otherwise your best bet is to have a LocalScript inside each button and when its clicked you gotta loop through all the frames and close them like I mentioned in my previous post.

https://developer.roblox.com/en-us/api-reference/property/GuiObject/ZIndex