Waiting for players function causes the next round to loop incorrectly

so ive been working on this round system for a game i have been working on. the script works pretty good but ive noticed that when it runs the “waiting for players” bit of the code it will loop the script wrong the next round. it ends up running the “waiting for players” bit of the script even when “TotalPlrs” = 2 or more upon the next run. when waiting for players is not ran, the script works perfectly.

i have a huge feeling this might be a simple mistake or oversight but it doesnt hurt to get help anyways.

heres the script:

local TimeLabel = game.StarterGui.EventGui.Time
local StartSpawn = game.Workspace.GameSpawn
local SpawnLocations = game.Workspace.Lobby.SpawnLocations:GetChildren()
local Teams = game:GetService("Teams")
local FirstGame = true
local Check = true
local TotalPlrs = nil
local timer = 30
local LastAlivePlr = nil

spawn(function()
	while task.wait(.2) do
		TotalPlrs = #game.Players:GetChildren()
		print("current plrs: "..tostring(TotalPlrs))
	end
end)

local function StartRound()
	spawn(function()
		for i,plr in pairs(game.Players:GetChildren()) do
			TimeLabel.Text = "game in progress..."
			game.ReplicatedStorage.Bind.UpdateTimeText:Fire(TimeLabel.Text)

			plr.Character:PivotTo(StartSpawn.CFrame)
			plr.Team = Teams["Playing"]

			plr.Character.Humanoid.Died:Connect(function()
				plr.Team = Teams["Lobby"]
				game.ReplicatedStorage.Bind.PlrDied:Fire(plr)
				game.ReplicatedStorage.Remote.GameStart:FireClient(plr,false)
			end)
			--patch 1: prevents plrs from cheating by reseting when game starts
			plr.CharacterAdded:Connect(function()
				plr.Team = Teams["Lobby"]
				game.ReplicatedStorage.Bind.PlrDied:Fire(plr)
				game.ReplicatedStorage.Remote.GameStart:FireClient(plr,false)
			end)
			--------------------------------------------------------------------
		end
	end)
	game.ReplicatedStorage.Bind.GameStart:Fire(true)
	game.ReplicatedStorage.Remote.GameStart:FireAllClients(true)
end

local function StartGame()
	timer = 30
	local AlivePlrCount = nil 

	TimeLabel.Text = tostring(timer)
	game.ReplicatedStorage.Bind.UpdateTimeText:Fire(TimeLabel.Text)
	wait(1)
	for i = 1,timer,1 do
		TimeLabel.Text = tostring(timer - i)
		game.ReplicatedStorage.Bind.UpdateTimeText:Fire(TimeLabel.Text)
		wait(1)
	end

	--waiting for players
	if TotalPlrs <= 1 then
		TimeLabel.Text = "waiting for players..."
		game.ReplicatedStorage.Bind.UpdateTimeText:Fire(TimeLabel.Text)
		local Waiting = true
		while Waiting == true do
			task.wait(.2)
			warn("system paused: waiting for players")
			game.Players.PlayerAdded:Connect(function(plr)
				plr.CharacterAdded:Connect(function()
					TimeLabel.Text = "doin somethin rq.."
					game.ReplicatedStorage.Bind.UpdateTimeText:Fire(TimeLabel.Text)
					wait(5)
					Waiting = false
					Check = true
					StartRound()
				end)
			end)
		end
	end

	-- enough players
	if TotalPlrs > 1 then
		Check = true
		StartRound()
	end

	-- constantly check alive players
	spawn(function()
		while Check == true do
			task.wait(.2)
			-- update plrs in playing team
			for i,v in pairs(Teams["Playing"]:GetPlayers()) do
				AlivePlrCount = #Teams["Playing"]:GetPlayers()
				if AlivePlrCount == 1 then
					LastAlivePlr = Teams["Playing"]:GetPlayers()[1]
				end
			end
			--check for winner/tie
			if AlivePlrCount == 1 or AlivePlrCount == 0 then
				warn("plrs alive: "..tostring(AlivePlrCount))
				if AlivePlrCount == 1 then
					--winner
					TimeLabel.Text = LastAlivePlr.DisplayName.. " is the winner"
					game.ReplicatedStorage.Bind.UpdateTimeText:Fire(TimeLabel.Text)
					wait(3)
					if game.Players:GetPlayerFromCharacter(LastAlivePlr.Character) then --patch 2: winner leaves game causing system to break
						local randomSpawn = SpawnLocations[math.random(1, #SpawnLocations)]
						LastAlivePlr.Character:PivotTo(randomSpawn.CFrame)
						LastAlivePlr.Team = Teams["Lobby"]
					end
				elseif AlivePlrCount == 0 or AlivePlrCount == nil then
					--tie
					TimeLabel.Text = "tie lol"
					game.ReplicatedStorage.Bind.UpdateTimeText:Fire(TimeLabel.Text)
					wait(3)
				end
				--end geam
				game.ReplicatedStorage.Bind.GameStart:Fire(false)
				game.ReplicatedStorage.Remote.GameStart:FireAllClients(false)
				Check = false
				StartGame()
			end
		end
	end)
end

-- startup
if FirstGame == true then
	StartGame()
	FirstGame = false
end```

Hey @IoIJay_JayIoI, I think it’s because you don’t check if there are enough players in TotalPlrs

Your script:


What i changed:

I added the line – I check if there are enough players

if TotalPlrs > 1 then

and I removed – because it seems unnecessary

game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function()
	--waiting for players
	if TotalPlrs <= 1 then
		TimeLabel.Text = "waiting for players..."
		game.ReplicatedStorage.Bind.UpdateTimeText:Fire(TimeLabel.Text)
		local Waiting = true
		while Waiting == true do
			task.wait(.2)
			warn("system paused: waiting for players")
			if TotalPlrs > 1 then -- if the number of players is satisfactory, start the game
					TimeLabel.Text = "doin somethin rq.."
					game.ReplicatedStorage.Bind.UpdateTimeText:Fire(TimeLabel.Text)
					wait(5)
					Waiting = false
					Check = true
					StartRound()
				end)
			end)
		end
	end

Ive forgot to close this post sorry, i have found a solution. sorry for any wasted time or efforts i will avoid this in a future.