Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

What is a working way to get all available guis but one (the one you want to open) to be invisible?

Title says what i want to accomplish (Local Script) My code:

local StarterGui = game.StarterGui
local gbg = StarterGui.Gamepasses.GamepassesBG
local ibg = StarterGui.Islands.IslandsBG
local pbg = StarterGui.PointsStatistics.PointsBG
local sbg = StarterGui.HelpfulMenu.Settings.SettingsBG
local hbg = StarterGui.HelpfulMenu.Help.HelpBG
script.Parent.MouseButton1Click:Connect(function()
	game.workspace["Music/Sounds"].OpenGui:Play()
	if gbg.Visible == false then
		gbg.Visible = true
		hbg.Visible = false
		ibg.Visible = false
		pbg.Visible = false
		sbg.Visible = false
	elseif gbg.Visible == true then
		gbg.Visible = false
		hbg.Visible = false
		ibg.Visible = false
		pbg.Visible = false
		sbg.Visible = false
	end
end)
game.Workspace["Music/Sounds"].OpenGui:Stop()

1 answer

You could use an In Pairs loop to close all of the Guis and make visible the one you wanted after, something like this:

local PlayerGui = game.Players.LocalPlayer:WaitForChild("PlayerGui")
local CurrentGui = script.Parent.Parent... --Reference the Gui you want to open using "script.Parent" until the frame is referenced

script.Parent.MouseButton1Click:Connect(function()
	for _, Gui in pairs(PlayerGui:GetDescendants()) do -- This goes through all the instances inside PlayerGui
		if Gui:IsA("Frame") then -- checking if "Gui" is a frame and not something else like a ScreenGui
			Gui.Visible = false
		end
	end
	
	for _, Frame in pairs(CurrentGui:GetDescendants()) do -- Getting all the instances under the Gui you want to make visible to make sure any frames are toggled
	if Frame:IsA("Frame") then -- Same as the last in pairs loop
		Frame.Visible = true
	end
end)

As a quick note: don't use StarterGui when referencing anything relating to Guis since StarterGui is similar to ReplicatedStorage, it clones all of your Guis into the player's screen, if you want to reference the player's individual Guis use PlayerGui (game.Players.LocalPlayer.PlayerGui) and don't forget to use WaitForChild() to avoid referencing Instances inside PlayerGui that ROBLOX hasn't finished loading yet (WaitForChild("GamepassesBG")).

This discussion is preserved as it was. New answers, comments, and votes are closed.