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