GUI not destroying after recreating itself

I’m using two different GUIs triggered by RemoteEvents. One shows “Waiting for players” before the game starts, and the other shows a countdown “Starting game… (10)” when enough players join. The countdown GUI is supposed to destroy the waiting GUI before showing.

The problem is if the player count drops below the minimum and then rises again, the waiting GUI sometimes doesn’t get destroyed when switching back to the countdown. It works fine the first time but breaks after players leave and rejoin quickly.

Side note: When it fails to destroy itself, it only shows for the players who have been in the game while the GUIs were switching back and forth. Not the new players that just joined.

local function showCountdown(seconds)
	waitscreen:Destroy()
	countdownscreen = Instance.new("ScreenGui")
	countdownframe = Instance.new("Frame")
	countdownlabel = Instance.new("TextLabel")
	wait(.1)
	countdownscreen.Name = "CountdownGui"
	countdownscreen.Parent = playerGui
	countdownscreen.ZIndexBehavior = Enum.ZIndexBehavior.Sibling

	countdownframe.Size = UDim2.new(0.6, 0, 0.08, 0)
	countdownframe.Position = UDim2.new(0.5, 0, 0.05, 0)
	countdownframe.AnchorPoint = Vector2.new(0.5, 0)
	countdownframe.BackgroundTransparency = 1
	countdownframe.Parent = countdownscreen

	countdownlabel.Size = UDim2.new(1, 0, 1, 0)
	countdownlabel.BackgroundColor3 = Color3.new(0, 0, 0)
	countdownlabel.BackgroundTransparency = 0.3
	countdownlabel.TextColor3 = Color3.new(1, 1, 1)
	countdownlabel.TextStrokeTransparency = 0.5
	countdownlabel.BorderSizePixel = 0
	countdownlabel.TextScaled = true
	countdownlabel.Font = Enum.Font.GothamBold
	countdownlabel.Text = ""
	countdownlabel.Parent = countdownframe

	for i = seconds, 1, -1 do
		if not countdownlabel or not countdownlabel.Parent or not countdownlabel:IsDescendantOf(game) then
			warn("Countdown cancelled because there are not enough players.")
			break
		end

		countdownlabel.Text = "Starting game... (" .. i .. ")"
		wait(1)
	end
		countdownlabel.Text = "Starting..."
end

local function waiting()
	countdownscreen:Destroy()
	waitscreen = Instance.new("ScreenGui")
	waitframe = Instance.new("Frame")
	waitlabel = Instance.new("TextLabel")
	wait(.1)	
	waitscreen.Name = "CountdownGui"
	waitscreen.Parent = playerGui
	waitscreen.ZIndexBehavior = Enum.ZIndexBehavior.Sibling

	waitframe.Size = UDim2.new(0.6, 0, 0.08, 0)
	waitframe.Position = UDim2.new(0.5, 0, 0.05, 0)
	waitframe.AnchorPoint = Vector2.new(0.5, 0)
	waitframe.BackgroundTransparency = 1
	waitframe.Parent = waitscreen

	waitlabel.Size = UDim2.new(1, 0, 1, 0)
	waitlabel.BackgroundColor3 = Color3.new(0, 0, 0)
	waitlabel.BackgroundTransparency = 0.3
	waitlabel.TextColor3 = Color3.new(1, 1, 1)
	waitlabel.TextStrokeTransparency = 0.5
	waitlabel.BorderSizePixel = 0
	waitlabel.TextScaled = true
	waitlabel.Font = Enum.Font.GothamBold
	waitlabel.Text = "Waiting for players..."
	waitlabel.Parent = waitframe
end

You might want to design the GUIs via the main editor instead of instantiating them through a script, then ticking ScreenGui.Enabled for the countdown and waiting screens.

1 Like

No differance at all.
Plus i assume he uses replicated first and if so then you are even more wrong becouse your idea will hurt load time badly.

visual demonstration? (can we see how it works when run?)

What would the difference be with using StarterGui versus ReplicatedStorage both in terms of stability of the game’s logic and load times? Or am I missing something here?

I’m sure that designing the UI once and replicated multiple times does not hurt load times, especially with ResetOnSpawn disabled. And even then, instantiating the UI through code to have the screens’ visibility change would have a negligible difference in performance and load times.

yes i had to record on my phone cause my laptops really slow for videos lol but look

this, literally this

just have the guis stored in something like ReplicatedStorage and clone them when needed, what you’re doing is extremely unnecessary and can be easily avoided

have a screen gui inside startergui, and then just parent what you need to it at runtime, there’s no need to create everything every time

then everytime the timer starts, you can just do yourGui:ClearAllChildren() to clear previous guis
also, what’s the point of wait(.1)??

1 Like

thank you i just tried this and it worked :smiley:

1 Like