How to check if only 1 person is alive

  1. What do you want to achieve?
    Hello! I am making a round based system for my game, and I am trying to make it so that if there is only 1 player left alive, the round would end and they would be the winner.

  2. What is the issue?
    The round just ends after it starts.

  3. What solutions have you tried so far?
    I have tried putting the amount of players left in a table, however that doesnt work.

Here are some snippets of the code

for _, Player in pairs(game.Players:GetPlayers())do
...
local players = game:GetService("Players"):GetPlayers()
table.insert(Playing, table.find(Playing, players))
for _, Player in pairs(game.Players:GetChildren()) do
...
if Player.Character and Player.Character:FindFirstChild("Humanoid") then
Player.Character.Humanoid.Died:Connect(function()
table.remove(Playing, table.find(Playing,Player))

...

if #Playing <= 1 then
print("a player has won")

Any help is appreciated, thank you!

1 Like
for _, Player in pairs(Playing) do


   if #Player = 1 then
   print(Playing, "Has won")
  end
end)
2 Likes

It doesn’t seem to work. Here’s where I put it if you need more info.

	for i = 60, 0, -1 do
		Status.Value = i
		task.wait(1)
		for _, Player in pairs(game.Players:GetChildren()) do
			if Player.Character and Player.Character:FindFirstChild("Humanoid") then
				connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
			table.remove(Playing, table.find(Playing,Player))
			end)
		end
		game.Players.PlayerRemoving:Connect(function(player)
				local ind = table.find(Playing, player)
				if ind then
					table.remove(Playing, ind)
					end
				end)
			end
			for _, Player in pairs(Playing) do
				if #Player == 1 then
					print(Playing, "Has won")
					RoundEndSFX:	Play()
					MusicStart:Stop()
					MusicLoop:Stop()
					break
				end
		end
		
	end
for i = 60, 0, -1 do
		Status.Value = i
		task.wait(1)
		for _, Player in pairs(game.Players:GetPlayers()) do
			if Player.Character and Player.Character:FindFirstChildOfClass("Humanoid") then
				connections[Player.Name] = Player.Character.Humanoid.Died:Connect(function()
			table.remove(Playing, table.find(Playing,Player))
			end)
		end
		game.Players.PlayerRemoving:Connect(function(player)
				local ind = table.find(Playing, player)
				if ind then
					table.remove(Playing, ind)
					end
				end)
			end
			for _, Player in pairs(Playing) do
				if #Player == 1 then
					print(Playing, "Has won")
					RoundEndSFX:Play()
					MusicStart:Stop()
					MusicLoop:Stop()
					break
				end
		end
		
	end
1 Like

Looking at it from a high level, I would create a property attribute for each player via leaderstats, then change that property every time a player dies. Create a loop that goes through all of players in game:GetPlayers() every couple seconds, and create a running count for each iteration of the loop which tracks how many are alive. When the count is just one, go through the list of players again, and when you found the player with the property, you can store it in a different variable and use it as needed.
You can clear out all the values after each round ends.

2 Likes

I solved a similar issue for someone a while back and it might help you out, just remove anything unnecessary.

1 Like

I would probably just connect Humanoid.Died & Player.CharacterRemoving functions to each player and check the players that are in the round each time.

1 Like

I’m kind of dumb so I don’t know if this would work.

You could make a value called players alive, and when the round starts you check how many people there are and make the value the amount of people. Then, make a value called in game that can be inside each player and so when the game starts it is true and when they die it’s set to false. Then, when the player dies the value is set to false and the number of players value decreases. This would repeat until it detects there is one player left.

1 Like

That’s along the lines of what I was thinking, except instead of checking player count, I would probably just add 1 to each whenever you tp the player or wtv so that you can have some kind of feature that allows some players to not join.

1 Like