Game is supposed to end if there is 0 players playing but it ends when only one player dies

Hello fellow developers,

I’m currently working on a round based gamewith a main script that chooses a random map and teleports players to it, that all work fine but im having issues with the part of the script that is supposed to stop the game if there are 0 players but it currently already stops the game when one player resets. I hope that someone can help me! Here is the round script:

local lobby = game.Workspace.Lobby
local Maps = game.ReplicatedStorage.Maps:GetChildren()
local Status = game.ReplicatedStorage.Status
local Players = game:GetService("Players")

local Playing = {}

--Checks how many players
local function enoughPlayers()
	return #Players:GetPlayers() >= 2
end

--Main round loop
while true do
	
		for i = 10, 0, -1 do
			Status.Value = "Intermission: "..i
			task.wait(1)
		end
		
		--Chooses random map
		local chosenMap = Maps[math.random(1, #Maps)]
		local clonedMap = chosenMap:Clone()
		
		clonedMap.Parent = game.Workspace
		Status.Value = "Map: "..clonedMap.MapName.Value
		
		task.wait(3)
		
		--Teleport players to map
		for i, Player in pairs(game.Players:GetPlayers()) do
			local character = Player.Character
			table.insert(Playing, Player)
			print(Playing)
			if character then
				character:MoveTo(clonedMap.TeleportPoint.Position)
			end
		end
		
		--Game countdown
		for i = 30, 0, -1 do
			Status.Value = "Game: "..i
			for i, Player in pairs(game.Players:GetPlayers()) do
				local character = Player.Character
				if character.Humanoid.Health <= 0 then
					local Index = table.find(Playing, Player)
					table.remove(Playing, Index)
					print(Playing)
				end
			end
			
			--If no more players end the game
			if #Playing == 0 then
				Status.Value = "Game over!"
				task.wait(3)
				break
			end 
			task.wait(1)
		end
		
		--Teleports players to lobby
		for i, Player in pairs(game.Players:GetPlayers()) do
			local character = Player.Character
			if character then
				character:MoveTo(lobby.TeleportPoint.Position)
			end
		end
		
		-- destroys map and cycle repeats
		clonedMap:Destroy()
	end

Thanks for reading!

You’re currently only checking to see how many players are within the Playing array (which means the players that are playing the current round), but you aren’t checking to see how many players are still joined to the “main” game (the actual game which exists outside of the rounds)


I’ve just realised that you were, in fact, breaking just the for loop rather than the core loop itself… The way I had the script zoomed in made it seem as though the if statement was running within the scope of the core loop :sweat_smile:

I’m glad that BonesIsUseless was able to find a solution to your problem :slight_smile::+1:

well i barely looks at your code and i’m already assuming the issue is with your game countdown

when you search for a player in Playing, you are always removing the next player because table.find() returns nil

table.remove(table, nil) is the same as table.remove(table, #table)

this means a player dies, you check that the player is dead, you remove the next player since the dead player isn’t Playing

Sorry i don’t fully understand(im kinda new to scripting). Should i check for players outside the round?

why exactly would the total player count be relevant to the early game end?

Hello,

sorry if im not as quick im just a beginner scripter. So what could be a possible solution to this? Should i handle the table.find differently?

Essentially, you shouldn’t break the core loop after all players have died within the round (as a matter of fact, the core loop shouldn’t be broken unless the server is empty). This is something best done using coroutines, I’ll write an example for you :slight_smile::+1:


@BonesIsUseless The core loop should only break once the server is empty, not because the round has ended


@groenedraak10

I’m done making the script :grin:

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Workspace = game:GetService("Workspace")

local lobby = Workspace:WaitForChild("Lobby")

local maps = ReplicatedStorage:WaitForChild("Maps"):GetChildren()
local status = ReplicatedStorage:WaitForChild("Status")


local connections = {}
local numberOfPlayers = 0
local thread = nil


local function disconnectConnections()
	for _, connection in connections do
		connection:Disconnect()
	end

	connections = {}
end

local function coreLoop()
	while true do
		for i = 10, 1, -1 do
			status.Value = "Intermission: "..i
			task.wait(1)
		end

		local playersInRound = {}

		local randomMap = maps[math.random(1, #maps)]:Clone()
		randomMap.Parent = Workspace

		status.Value = "Map: "..randomMap.MapName.Value
		task.wait(3)

		for _, player in Players:GetPlayers() do
			table.insert(playersInRound, player)

			if player.Character then
				player.Character:MoveTo(randomMap.TeleportPoint.Position)

				local humanoid = player.Character:FindFirstChildOfClass("Humanoid")

				if humanoid then
					table.insert(connections, humanoid.Died:Connect(function()
						table.remove(playersInRound, player)
					end))
				else
					table.remove(playersInRound, player)
				end
			end
		end

		for i = 30, 1, -1 do
			status.Value = "Game: "..i
			task.wait(1)

			if #playersInRound == 0 then break end -- All players have died, so exit out of the for loop
		end

		for _, player in Players:GetPlayers() do
			if player.Character then
				player.Character:MoveTo(lobby.TeleportPoint.Position)
			end
		end

		task.spawn(disconnectConnections)

		randomMap:Destroy()
	end
end

local function onPlayerAdded(player: Player)
	numberOfPlayers += 1

	if numberOfPlayers >= 2 and thread == nil then
		thread = task.spawn(coreLoop)
	end
end

local function onPlayerRemoving(player: Player)
	numberOfPlayers -= 1

	if numberOfPlayers == 0 and thread then
		task.cancel(thread)
		thread = nil

		disconnectConnections()
	end
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

a simple fix would be checking if the dead player is Playing or not

Index is supposed to represent a player Playing
so check if Index is real (if it’s real, the player is Playing)

if not Index then continue end

if Index is not real, then the player is not Playing (because he was Playing, but died, then got removed from Playing)

Where should i put this line? Sorry im kinda slow lol

surely you could figure it out

i’m gonna go eat

Thanks for the help i figured it out!

Thanks but i already solved it with the help of @BonesIsUseless