My game breaks when a player joins in the middle of the round

I have 0 clue why this happens

I have an idea it happens when I check the players in the round count.

An idea i have is that at the beginning of each round create a new table with all the players in the rounds name.

local firstLayer = workspace.firstLayer


local playersLeft = 0


local colorInGame = false



local colors = {
	BrickColor.new("Camo"),
	BrickColor.new("Artichoke"),
	BrickColor.new("Pastel light blue"),
	BrickColor.new("Deep blue"),
	BrickColor.new("Dark indigo"), -- 5
	BrickColor.new("Alder"),
	BrickColor.new("Cadet blue"),
	BrickColor.new("Teal"),
	BrickColor.new("Crimson"),
	BrickColor.new("Persimmon"), -- 10
	BrickColor.new("Deep orange"),
	BrickColor.new("Bright orange"),
	BrickColor.new("Dusty Rose"),
	BrickColor.new("Rust"),
	BrickColor.new("Mid gray") -- 15
}                





while true do
	
	for i = 15, 0, -1 do
		game.ReplicatedStorage.statusText.Value = "INTERMISSION: " .. i 
		wait(1)
	end
	
	game.ReplicatedStorage.statusText.Value = "GAME ON-GOING"
	
	for _, v in pairs(game.Players:GetPlayers()) do
		v.Character.HumanoidRootPart.CFrame = CFrame.new(math.random( 10.5, 30.5), 150, math.random( -200, -185))
		v.varibles.inRound.Value = true 
	end
	
	while true do
		playersLeft = 0      --seting variable
		
		for _, v in pairs(firstLayer:GetChildren()) do
			local color = colors[math.random(1, #colors)]
			v.BrickColor = color
		end
		for _, v in pairs(workspace.secondLayer:GetChildren()) do
			local color = colors[math.random(1, #colors)]
			v.BrickColor = color
		end
		
		for _, v in pairs(workspace.thirdLayer:GetChildren()) do
			local color = colors[math.random(1, #colors)]
			v.BrickColor = color
		end
		
	
		
		local chosenColor = colors[math.random(1, #colors)]   --getting random index from a table
		
		game.ReplicatedStorage.colorTeller:FireAllClients(chosenColor) -- sending the color to a ui
		
		wait(5)
		
		
		for _, v in pairs(firstLayer:GetChildren()) do

			if v.BrickColor ~= chosenColor then
				
				v.Transparency = 1
				v.CanCollide = false
			end
		end
		
		for _, v in pairs(workspace.secondLayer:GetChildren()) do

			if v.BrickColor ~= chosenColor then
				v.Transparency = 1
				v.CanCollide = false
			end
		end
		
		for _, v in pairs(workspace.thirdLayer:GetChildren()) do

			if v.BrickColor ~= chosenColor then
				v.Transparency = 1
				v.CanCollide = false
			end
		end
		
		
		for i, v in pairs(workspace.levelBarriers:GetChildren()) do
			v.CanCollide = true --making the barriers between level collideable
		end
		wait(5)
		
		for i, v in pairs(workspace.levelBarriers:GetChildren()) do
			v.CanCollide = false 
		end
		
		for _, v in pairs(firstLayer:GetChildren()) do
			v.Transparency = 0
			v.CanCollide = true
		end
		for _, v in pairs(workspace.secondLayer:GetChildren()) do
			v.Transparency = 0
			v.CanCollide = true
		end

		for _, v in pairs(workspace.thirdLayer:GetChildren()) do
			v.Transparency = 0
			v.CanCollide = true
		end
		
		for _, v in pairs(game.Players:GetPlayers()) do --seeing how many players are left
			if v.varibles.inRound.Value == true then
				playersLeft = playersLeft + 1 
				print(playersLeft)
			end
		end
		
		print(playersLeft)
		
		if playersLeft <= 1 then I THINK THE ERROR IS WITH THIS. 
			break
		end
		
		
	end 
	
	playersLeft = 0 
	
	for _, v in pairs(game.Players:GetPlayers()) do
		if v.varibles.inRound.Value == true then
			
			game.ReplicatedStorage.winner:FireClient(v)
			v.varibles.wins.Value = v.varibles.wins.Value + 1
		else
			game.ReplicatedStorage.loser:FireClient(v)
			v.varibles.losses.Value = v.varibles.losses.Value + 1 
		end
		local spawnPoint = workspace.Spawns:GetChildren()[math.random(1,#workspace.Spawns:GetChildren())]
		v.Character.HumanoidRootPart.CFrame = spawnPoint.CFrame + Vector3.new(0, 5, 0)
		v.varibles.inRound.Value = false
	end
	
	
end


You need to be more descriptive of what you’re trying to achieve.
From a reader’s perspective, all we see is some code that doesn’t work, we have no idea what you’re trying to accomplish.

1 Like