Gui closing/opening

This is for someone else who can’t post yet so.

  1. I’m creating a main menu GUI and within the main menu I am creating a team selector and I want the gui to fully close.

  2. The issue is that whenever I execute my script, it only closes the frame parenting my image button leaving the main menu tab open I have a feeling it might be the organization of my gui or maybe the script.

  3. I have tried chat GPT also tweeking the script by myself and it didnt solve the issue

Image

local Players = game:GetService(“Players”)
local Teams = game:GetService(“Teams”)

local player = Players.LocalPlayer
local gui = player.PlayerGui:WaitForChild(“ScreenGui”)

local function closeFrames(frame)
– Close the given frame and all its descendants
for _, child in ipairs(frame:GetDescendants()) do
if child:IsA(“Frame”) then
closeFrames(child)
end
end
frame:Destroy()
end

local function assignTeam()
– Assign a team to the player
local team = Teams:GetRandomTeam()
player.Team = team

-- Close all frames under the MainFrame
local mainFrame = gui:FindFirstChild("MainFrame")
if mainFrame then
    closeFrames(mainFrame)
end

end

– Find the nested button within the GUI
local nestedButton = gui:WaitForChild(“MainFrame”):WaitForChild(“NestedButton”)

– Connect the nested button to the assignTeam function
nestedButton.MouseButton1Click:Connect(assignTeam)

3 Likes

uh, why dont you jsut disable the ui?

1 Like

As @WickdSuper says, just disable it, hide it, move it offscreen or whatever. No point in destroying it if you need it to be persistent.

just do

plr = game.Players.LocalPlayer
local function function name here()
local ui = plr.PlayerGui:FindFirstChild("UINameHere")
if ui then 
ui.Enabled = false
else
print(error("No Gui found!"))
end
end

edit: i changed some of the script to make sure theres a ui

1 Like

He said to me: Sorry for the confusion, I meant i want the GUI to disable fully after a player team is selected under the team selection tab, the script in the post is the script of me attempting to have the GUI close after a team is selected.

yeah, i just gave you a script, wdym by this?

he said, that this makes it disable the ui once you join, he needs it after the event happens. Hes stuck on making it close the ui after you mouse click the image button.

i made a function, you can just use that function in the script (on mousebutton1click, or mousebutton1down wtv)

You don’t have to access the UI by Local player, it’s so unnecessary to disable the GUI through player if you don’t have remotes to fire or cloning the GUI container to player GUI.

Client is already having access to the GUI and it works by just parenting it with script.Parent, it doesn’t really affect to all clients outside except in the current client (the devices we are using)

Disabling GUI container would be your solution when you don’t need them now, scripts however will still run if it doesn’t get disabled yet.

--script inside a GUI container (ScreenGui)
local GUI = script.Parent
GUI.Enabled = false --completely hidden

also i recommend merging those script in one line as they work without having to place them in each buttons, they don’t only run in a button as long as you assign them correctly

1 Like

its ok now he fixed it. Thank you to everyone who helped :slight_smile: