Countdown script duplicates when another player joins and gui player script is messed up

I’m trying to make a countdown script it works fine but when another account joins the script duplicates and the countdown subtracts by 2 instead of 1

The script is in ServerScriptService


wait(5) ---Wait Until The Server Starts

while true do
	wait(0.25)
	script.Count.Value = 60 --- 60
	for i = 1,50 do --- 50
		wait(1)
		for i, v in pairs(game.Players:GetPlayers()) do
			local pgui = v:WaitForChild("PlayerGui")
			pgui.BossCountdown.Frame.Visible = true
			script.Count.Value -= 1
			pgui.BossCountdown.Frame.TextLabel.Text = "Boss In: " .. script.Count.Value .. " Seconds"
		end
	end
	for i = 1,10 do
		wait(1)
		for i, v in pairs(game.Players:GetPlayers()) do
			local pgui = v:WaitForChild("PlayerGui")
			pgui.BossCountdown.Frame.Visible = true
			script.Count.Value -= 1
			ss.Countdown:Play()
			pgui.BossCountdown.Frame.TextLabel.Text = "Boss In: " .. script.Count.Value .. " Seconds"
		end
	end
	for i, v in pairs(game.Players:GetPlayers()) do
		local pgui = v:WaitForChild("PlayerGui")
		pgui.Boss.Frame.Enabled.Value = true
	end
	script.Count.Value = 180 --- 180
	for i, v in pairs(game.Players:GetPlayers()) do
		local pgui = v:WaitForChild("PlayerGui")
		pgui.BossCountdown.Frame.Visible = true
		pgui.BossCountdown.Frame.TextLabel.Text = "Boss Is Starting!"
	end
	wait(0.05)
	for i, v in pairs(game.Players:GetPlayers()) do
		local pgui = v:WaitForChild("PlayerGui")
		pgui.BossCountdown.Enter.Visible = true
	end
	wait(6.5)
	for i, v in pairs(game.Players:GetPlayers()) do
		local pgui = v:WaitForChild("PlayerGui")
		pgui.BossCountdown.Enter.Visible = false
	end
	for i = 1,170 do --- i = 1,170
		wait(1)
		for i, v in pairs(game.Players:GetPlayers()) do
			local pgui = v:WaitForChild("PlayerGui")
			pgui.BossCountdown.Frame.Visible = true
			script.Count.Value -= 1
			pgui.BossCountdown.Frame.TextLabel.Text = "Boss Ending In: " .. script.Count.Value .. " Seconds"
		end
	end
	for i = 1,10 do
		wait(1)
		for i, v in pairs(game.Players:GetPlayers()) do
			local pgui = v:WaitForChild("PlayerGui")
			pgui.BossCountdown.Frame.Visible = true
			script.Count.Value -= 1
			ss.Countdown:Play()
			pgui.BossCountdown.Frame.TextLabel.Text = "Boss Ending In: " .. script.Count.Value .. " Seconds"
		end
	end
	for i, v in pairs(game.Players:GetPlayers()) do
		local pgui = v:WaitForChild("PlayerGui")
		pgui.Boss.Frame.Enabled.Value = false
	end
	script.Count.Value = 30 --- 30
	for i = 1,20 do --- 20
		wait(1)
		for i, v in pairs(game.Players:GetPlayers()) do
			local pgui = v:WaitForChild("PlayerGui")
			pgui.BossCountdown.Frame.Visible = true
			script.Count.Value -= 1
			pgui.BossCountdown.Frame.TextLabel.Text = "Intermission: " .. script.Count.Value 
		end
	end
	for i = 1,10 do
		wait(1)
		for i, v in pairs(game.Players:GetPlayers()) do
			local pgui = v:WaitForChild("PlayerGui")
			pgui.BossCountdown.Frame.Visible = true
			script.Count.Value -= 1
			ss.Countdown:Play()
			pgui.BossCountdown.Frame.TextLabel.Text = "Intermission: " .. script.Count.Value
		end
	end
end

I also have another error that I’m trying to make a player system it looks like this

Everything works when im playing solo but when another player joins it my player doesn’t show up and when im trying to control the other player it says this in the output

also on my main my player sometimes doesn’t show up

You might have to add a line that waits for the Character to be added.

Is this the entire script? Are you adding the script to the player when a player joins? If so, can we see that code?

That’s the entire script for the countdown the value changes in that script

looks like you’re subtracting 1 from the CountValue for every single player because it loops through all the players and subtracts 1 for each player.
Here’s a simplified version of what you’re doing

for i = 1, 10 do
	wait(1)
	for _, v in pairs(game:GetService("Players"):GetChildren()) do
		countValue = countValue - 1 -- this will subtract 1 every time it finds a new player
	end
end

what you want to do be doing is something like this:

for i = 1, 10 do
	wait(1)
	countValue = countValue - 1 -- this will subtract 1 every second now
	for _, v in pairs(game:GetService("Players"):GetChildren()) do
		-- update player visuals to match new count
	end
end

I fixed the countdown subtracting by 2 but its easy for the script to stop due to the player not loading fast enough

ok I fixed the script stopping by removing

wait(5) ---Wait Until The Server Starts

1 Like