Intermission script breaks when player joins at the end of intermission

Hello! I am creating a game with a round-based system that has an intermission timer and waits for one player to be remaining in the round. The round then ends and teleports all players back to the spawn.

There is only one slight problem with this. The intermission script breaks when a new player joins at the end of the intermission countdown. For example, a player is joining the game and happens to spawn in right when intermission hits zero. In this case, the round system stops for all players and will not continue until the server is closed.

I am not sure if game.Players:GetPlayers() is causing this issue, as there are no other posts on this topic on the developer forum.

If you need any more information, do not hesitate to ask. My code is shown below.

function changeColors()
	for i, v in pairs(game.Workspace.Tiles:GetChildren()) do
		local colorIndexSel = math.random(1, #game.Workspace.TileColors:GetChildren())
		v.Color = game.Workspace.TileColors:GetChildren()[colorIndexSel].Value
		v.Name = game.Workspace.TileColors:GetChildren()[colorIndexSel].Name
	end
end

coroutine.wrap(function()
	while true do
		local intermissionTime = 15
		while intermissionTime >= 0 do
			game.ReplicatedStorage.UpdateStatus:FireAllClients("Intermission: ".. intermissionTime)
			wait(1)
			intermissionTime = intermissionTime - 1
		end
		
		players = {}
		
		if #game.Players:GetPlayers() < 2 then
			game.ReplicatedStorage.UpdateSecondaryStatus:FireAllClients("At least two players are required to start the game.")
			wait(5)
			game.ReplicatedStorage.UpdateSecondaryStatus:FireAllClients("")
		else
			changeColors()
			
			for i, v in pairs(game.Players:GetPlayers()) do
				v.Character.HumanoidRootPart.Position = Vector3.new(game.Workspace.Tiles:GetChildren()[i].Position.X, game.Workspace.Tiles:GetChildren()[i].Position.Y + 4, game.Workspace.Tiles:GetChildren()[i].Position.Z)
				table.insert(players, v)
			end
			
			print(players)
			
			while #players > 1 do
				local keepColorCountdown = 5

				local colorIndexSel = math.random(1, #game.Workspace.TileColors:GetChildren())
				local keepColor = game.Workspace.TileColors:GetChildren()[colorIndexSel].Name
				local keepColorValue = game.Workspace.TileColors:GetChildren()[colorIndexSel].Value
				keepColorValue = keepColorValue:ToHex()

				while keepColorCountdown >= 0 do
					game.ReplicatedStorage.UpdateStatus:FireAllClients("Stand on <font color='#".. keepColorValue .."'>".. keepColor .."</font>! ".. keepColorCountdown)
					wait(1)
					keepColorCountdown = keepColorCountdown - 1
				end
				
				for i, v in pairs(game.Workspace.Tiles:GetChildren()) do
					if keepColor ~= v.Name then
						v.Transparency = 1
						v.CanCollide = false
					end
				end
				
				game.ReplicatedStorage.UpdateStatus:FireAllClients("Stand on <font color='#".. keepColorValue .."'>".. keepColor .."</font>!")
				wait(3)
				
				for i, v in pairs(game.Workspace.Tiles:GetChildren()) do
					v.Transparency = 0
					v.CanCollide = true
				end
				
				changeColors()
			end
			
			players = {}
			print("Cleared 'players' table:")
			print(players)
		end
		
		for i, v in pairs(game.Workspace.Tiles:GetChildren()) do
			v.BrickColor = BrickColor.new("Medium stone grey")
			v.Name = "TilePart"
		end
	end
end)()

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			print(player.Name.. " has died!")
			if table.find(players, player) ~= nil then
				table.remove(players, table.find(players, player))
				print(players)
				
				game.ReplicatedStorage.UpdateSecondaryStatus:FireAllClients(player.Name.. " has been eliminated!")
				
				if #players == 1 then
					local winner = players[1]
					
					winner.Character.HumanoidRootPart.Position = Vector3.new(game.Workspace.SpawnLocationMain.SpawnLocation.Position.X, game.Workspace.SpawnLocationMain.SpawnLocation.Position.Y + 4, game.Workspace.SpawnLocationMain.SpawnLocation.Position.Z)
					game.ReplicatedStorage.UpdateSecondaryStatus:FireAllClients(winner.Name.. " has won this round!")
					
					local playerImage, isReady = game.Players:GetUserThumbnailAsync(winner.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size420x420)
					local winnerBoard = game.Workspace["Map Assets"].WinnerBoard.Board.SurfaceGui
					
					winnerBoard.CharacterPicture.Image = playerImage
					winnerBoard.PlayerName.Text = winner.Name
					
					winnerBoard.Enabled = true
				end
			else
				print(player.Name.. " does not exist in 'players'")
			end
		end)
	end)
end)

What’s the error message you get when a player joins?

I do not remember if there was an error. I don’t think there was one, but I could be wrong. I do not have anyone to help me test it either.

Also, the script breaks only when a player joins when the intermission timer hits zero.

That sounds like an exceptionally rare occurrence. Try to get the error message from it happening, otherwise I don’t see any issues.

Hmm. This has happened multiple times when I was testing with my friends.