How to check how many players are still alive in a match?

Hello!

I’m trying to figure out how to check how many players are still living in a round.

For example:
Becky, Mark, and Ruth spawn in to the arena. Becky and Ruth die. How does the game detect the Mark is the only player left alive? (Sorry if that was confusing.)

2 Likes

Create a table to keep track of all the players in-round. When a player dies, simply remove them from the table.

3 Likes

How do I do that? (I’m learning to script.)

1 Like

Please search on the developer API reference as this contains information for how to work with tables. You should at least give it a go writing a script yourself before asking on the devforum.

1 Like

Well, we can add players into a table and remove them if they die throughout the match or leave the game. I am going to just write a placeholder game script and show you what this would look like since you’ve said you’re learning to script.

local Players = game.Players

local playersToWin = 1 -- change this to whatever amount of players you want to win the game

while true do -- general loop for the match
	local playersInMatch = {} -- track alive players
	local connections = {} -- store connections to disconnect later
	for _, Player in pairs(Players:GetChildren()) do
		if Player.Character and Player.Character:FindFirstChild('Humanoid') and Player.Character.Humanoid.Health >= 1 then -- quick check to see if they are dead to avoid nil values

			table.insert(playersInMatch, Player) -- add all players to table
			connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function() -- remove player from list if they die
				table.remove(playersInMatch, table.find(playersInMatch, Player))
			end)
		end
	end
	connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInMatch, player)
		if ind then
			table.remove(playersInMatch, ind)
		end
	end)
	
	
	repeat wait(1) until #playersInMatch == playersToWin -- looping a check to see if the match has ended

	if #playersInMatch == playersToWin then -- checking if match is over
		print("Match Over")
	else
		print("There was NO winner.")
	end
end

This script I’ve just written should get you started. If you have any errors with this or need some guidance, I’d be happy to assist you. - Smith

10 Likes

If you’re lazy or are using teams, GetPlayers and use the length operator. If you’re still lazy but aren’t using teams, then CollectionService. Tag alive characters and check how many instances still exist in that tag with GetTagged. You can also manually manage tags on a Player instance for that. Still lazy but want a direct solution? Consider attributes. Want a proper way to do it? A table, like the example above.

Point is, there’s many ways to do this, but your fundamental issue lies in not tracking who enters the round to begin with. If you’re tracking players who enter a round them you should naturally be able to know who’s in the round and likewise be able to add or remove players from that table and then the solution naturally floats itself onto your lap, just check how many entries are in the table.

1 Like

Hey smith! Just saw this, can you tell me how to disconnect everything?

Hello Wizzly, what do you mean by “disconnect everything?”

I’m guessing that you are wondering how to remove players from the table, but the script automatically does this if they die or leave the game within these functions:

connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function() -- remove player from list if they die
				table.remove(playersInMatch, table.find(playersInMatch, Player))
			end)
connections["Removing"] = game.Players.PlayerRemoving:Connect(function(player) -- remove player from list if they leave
		local ind = table.find(playersInMatch, player)
		if ind then
			table.remove(playersInMatch, ind)
		end
	end)

If this doesn’t answer your question, let me know what else you need; I’m happy to help you. - smuff

--// Services //--
local Players = game:GetService("Players")

--// Tables //--
local AlivePlayers = {}
local PlayerList = Players:GetPlayers()

--// Functions //--
local function DetectAlivePlayers()
	for _, player in pairs(PlayerList) do
		if player.Character.Humanoid.Health > 0 then
			table.insert(AlivePlayers, player)
		end

		player.Character.Humanoid.Died:Connect(function()
			local DeadPlayer = table.find(AlivePlayers, player)
			
			table.remove(AlivePlayers, DeadPlayer)
		end)
	end
end

while true do
	DetectAlivePlayers()
	task.wait()
end
1 Like

That cleared my doubt! Sorry I wasnt clear, I appreciate you for your time :slight_smile:

1 Like