Help - left with a lot of ui but don't have a script

image

Hello, i don’t have a script that allows me to close a frame once i open another one. I don’t know how to code this? what do I do

you can use tables to store current opened Guis and loop through the table to disable the guis when opening a new one
example


local openGuis = {}


local function openGuiAndCloseOpenedGuis(gui :ScreenGui)
	-- loop and disable all current open guis
	for _, gui :ScreenGui in openGuis do
		gui.Enabled = false 
	end
	
	-- add this gui instance to the openGuis table and enable it
	table.insert(openGuis, gui) 
	gui.Enabled = true
end

local function closeGui(gui :ScreenGui)
	--close the gui and removes it from table
	table.remove(openGuis, gui)
	gui.Enabled = false
end
1 Like

i don’t know how to make it work?

local player = Players.LocalPlayer
local playerGui = player:WaitForChild("PlayerGui")

-- Table to track currently open GUIs
local openGuis = {}

-- References to frames and image label
local areasFrame = playerGui.HUD.Windows.Areas
local upgradesFrame = playerGui.HUD.Windows.Upgrades
local virtualShopFrame = playerGui.virtualShop.Frame
local bookImageLabel = playerGui.plantGui.book

-- Helper function to open a GUI and close others
local function openGuiAndCloseOpenedGuis(gui)
	-- Loop through and disable all currently open GUIs
	for _, openGui in ipairs(openGuis) do
		openGui.Visible = false -- Use Visible for Frames or ImageLabels
	end

	-- Clear the table and add the new GUI
	openGuis = {}
	table.insert(openGuis, gui)

	-- Enable the new GUI
	gui.Visible = true
end

-- Helper function to close a GUI
local function closeGui(gui)
	-- Remove the GUI from the openGuis table and disable it
	for i, openGui in ipairs(openGuis) do
		if openGui == gui then
			table.remove(openGuis, i)
			break
		end
	end
	gui.Visible = false -- Use Visible for Frames or ImageLabels
end

-- Example connections for toggling GUIs
-- Replace these with your actual triggers/buttons to open/close the frames
areasFrame.MouseButton1Click:Connect(function()
	openGuiAndCloseOpenedGuis(areasFrame)
end)

upgradesFrame.MouseButton1Click:Connect(function()
	openGuiAndCloseOpenedGuis(upgradesFrame)
end)

virtualShopFrame.MouseButton1Click:Connect(function()
	openGuiAndCloseOpenedGuis(virtualShopFrame)
end)

bookImageLabel.MouseButton1Click:Connect(function()
	openGuiAndCloseOpenedGuis(bookImageLabel)
end)

Whenever you open one set that ones name to a variable, then loop through all the ui using getdescendants and if it’s a valid frame whose name is not the one or the variable you set it’s visibility to false

1 Like

but what if you want a fancy little animation when it closes

@talis783

Use tween service, it’s very customizable. When looping you can check if it’s visible before doing anything and if it is play the tween on it. There are tons of videos on how to use tween service

1 Like