Why won't round system end?

Hi, I am making a round system for my game, but it doesn’t detect when the player count is 1 or 0. There are no errors. I think the issue is below END OF PLAYER DIED/REMOVED/LEFT comment. Can someone help? Here is my current code:

local RS = game.ReplicatedStorage
local SS = game.ServerStorage
local Maps = SS:WaitForChild("Maps")
local Status = RS:WaitForChild("Status")
local intermissionLength = 15
local Timer = game.ReplicatedStorage.Timer
local data = require(script.Parent["Questions/Answers"])
local highestNumber = 0
local chosen = nil

while true do
	Status.Value = "Waiting for enough players! (1/2)"
	repeat task.wait() until game.Players.NumPlayers >=2
	Status.Value = "Intermission:"

	task.wait(3)

	Status.Value = "15"
	for i=1,intermissionLength do
		task.wait(1)
		local new = tonumber(Status.Value)-1
		Status.Value = tostring(new)
	end
	--END OF INTERMISSION

	local plrs = {}

	for i,player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(plrs,player) --add plr

		end
	end

	local padVotes = {
		["Construction"] = tonumber(workspace.Lobby.Construction.SurfaceGui.TextLabel.Text),
		["Radioactive"] = tonumber(workspace.Lobby.Radioactive.SurfaceGui.TextLabel.Text),
		["City"] = tonumber(workspace.Lobby.City.SurfaceGui.TextLabel.Text)
	}

	for i, v in pairs(padVotes) do
		local number = v
		if number > highestNumber then
			highestNumber = number
			chosen = i
		end
	end

	local ChosenMap = tostring(chosen)

	task.wait(.3)

	if ChosenMap == nil or ChosenMap == "0" or ChosenMap == "nil" then
		local AvailableMaps = Maps:GetChildren()
		ChosenMap = tostring(AvailableMaps[math.random(1,#AvailableMaps)])
		Status.Value = tostring(ChosenMap).. " has been chosen!"
	else
		Status.Value = tostring(ChosenMap).. " has been chosen!"
	end

	task.wait(2)

	Status.Value = "Game starting!"

	task.wait(2)
	local ClonedMap = game.ServerStorage.Maps:FindFirstChild(ChosenMap):Clone()
	if ClonedMap ~= nil then
		if ClonedMap == "Radioactive" then
			ClonedMap:PivotTo(CFrame.new(490.263, 79.457, -4.343) * CFrame.Angles(0, 0, math.pi))
		elseif ClonedMap == "Construction" then
			ClonedMap:PivotTo(CFrame.new(490.263, 35.957, -4.343))
		elseif ClonedMap == "City" then
			ClonedMap:PivotTo(CFrame.new(490.263, 35.957, -4.343))
		end
	end
	ClonedMap.Parent = workspace

	local SpawnPoints = game.ServerStorage.Maps[ChosenMap]:FindFirstChild("SpawnPoints")

	if not SpawnPoints then
		warn("No spawnpoints found")
	end

	--END OF CHOOSING MAP

	local AvailableSpawns = SpawnPoints:GetChildren()

	for i,player in pairs(plrs) do
		if player then
			local char = player.Character

			if char then
				char:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawns[1].CFrame + Vector3.new(0,2,0)
				table.remove(AvailableSpawns,1)

				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = char
			else
				if not player then
					table.remove(plrs,i)
				end
			end
		end
	end

	--END OF TELEPORTING PLAYERS

	Status.Value = "Get ready to play!"

	for i,player in pairs(plrs) do
		if player then
			local character = player.Character

			if not character then
				print("Player left the game")
				table.remove(plrs,i)
			elseif character:FindFirstChild("GameTag") then
				print(player.Name.. " is still in the game!")
			else
				table.remove(plrs,i)
			end

		else table.remove(plrs,i)
			print(player.Name.. " has been removed")

		end
	end

	--END OF PLAYER DIED/REMOVED/LEFT
	Timer.Value = 15

	repeat
		if #plrs <= 1 then
			break
		end
		game.ReplicatedStorage.SafeAnswer:FireAllClients()

		local questionnum = math.random(#data)
		local question = data[questionnum].question

		Status.Value = question
		Timer.Value = 15

		for i=1,15 do
			task.wait(1)
			local new = Timer.Value-1
			Timer.Value = new
		end

		ClonedMap.Lava.Size += Vector3.new(0,10,0)
		if #plrs <= 1 then
			break
		end

	until #plrs <= 1 

	if #plrs == 1 then
		if plrs[1].Character:FindFirstChild("GameTag") then
			Status.Value = "The winner is: " ..plrs[1].Name.. "!"
			plrs[1].leaderstats.Wins.Value += 1
			Timer.Value = 180
		end
	elseif #plrs == 0 then
		Status.Value = "Nobody won!"
		Timer.Value = 180
	end

	Status.Value = "End of game!"
	task.wait(2)

	for i,player in pairs(game.Players:GetPlayers()) do
		local charac = player.Character

		if not charac then
			print("Player does not have character!")
		elseif charac then
			if charac:FindFirstChild("GameTag") then
				charac.GameTag:Destroy()
				player:LoadCharacter()
			end
		end
	end
	for i,v in pairs(workspace.StepsFolder:GetChildren()) do
		if v.Name == "Step" then
			v:Destroy()
		end
	end
	ClonedMap:Destroy()
	workspace.Lobby.Construction.SurfaceGui.TextLabel.Text = "0"
	workspace.Lobby.Radioactive.SurfaceGui.TextLabel.Text = "0"
	workspace.Lobby.City.SurfaceGui.TextLabel.Text = "0"
	Timer.Value = 180

	task.wait(3)
	--END OF GAME ENDING
end

This for loop should probably be inside your repeat until

i think whats happening is its only checking the players once and removes a few then gets stuck in the repeat and no more are ever removed

Also you may consider using ipairs instead of pairs on your tables

also u don’t need this as its what the until #plrs <= 1 does

1 Like

What’s the difference between pairs and ipairs? They both have same description in the roblox scripting description.

1 Like

ipairs runs faster its not much but if you used it in alot of loops it might matter and it also iterates in order so it goes through spot 1 of the table then 2 then 3

pairs is actually for going through a dictionary which is a table setup with string keys
like table[key] = value and pairs with just randomally kinda iterate through not in order and can change each time you go through the loop

but if you use ipairs on dictionary it will not loop or work since there is no order in the table

I am not the best at explaining stuff my brain works in mysterious ways but hope that helps

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.