Adding players to a table to detect when all players in a game die

I am aiming to create a game that is similar to The Lab, where each player gets a plate and then random things will happen to random player’s plates. The issue I am currently having is adding every player to a table at the start of a game, then detecting when one of the players in the game dies and then removing them from the table. Then when there is only one player remaining in the table, they win!

I’ve tried adding a player to a table when the game starts, but nothing I really attempted worked well in testing. How would I go about this?

if it helps, here is the current code I have. It creates a plate for each player in the game then teleports them to it!

-- find random child
function frc(model)
    return model:GetChildren()[math.random(1,#model:GetChildren())]
end

function begin()
    local players = game:GetService("Players"):GetChildren()
    local loop_times = #players
    while loop_times > 0 do
        local plate = frc(game:GetService("ReplicatedStorage").plates)
        local found = false
        while found == false do
            plr = frc(game:GetService("Players"))
            if plr.plate.Value == false then
                plr.plate.Value = true
                plate.Name = plr.Name.."'s plate"
                found = true
            end
        end
        found = false
        plate.Parent = workspace
        plr.Character:MoveTo(plate.Position)
        loop_times = loop_times - 1
    end
end

above code in action:
https://gyazo.com/d053943e6e27bc24256c8f976bcccd4f

local PlayersInRound = {}

game:GetService("Players").PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		if #PlayersInRound > 0 then
			for Position, Plr in pairs(Players:GetPlayers()) do
				if Player.Name == Plr.Name then
					table.remove(PlayersInRound, Position)
				end
			end)
		end
	end)
end)

--At Start Of Round
PlayersInRound = Players:GetPlayers()

You can then detect if any players are alive by doing

if #PlayersInRound == 0 then
	-- do your thing
end
4 Likes

For a basic solution, when the game starts, loop through all the players and add a folder to them called ‘Playing’, and remove it when they die.

Simply deal with checking if the table has 0 contents:

local emptytable = {}
local someothertable = {"player_a", "player_b"}

print(#emptytable) -- 0
print(#someothertable) -- 2

Of course you have to create a table out of current round like in an aforementioned post by @Kymaraaa.

At the time of writing, I was tired. :sleepy:

2 Likes

Thank you @Operatik and @Kymaraaa! Your solutions have helped, and it now works.

Some more code for thought.

local PlayersAlive = {}

local function RoundEnded()
	PlayersAlive = {}
	-- Do end round stuff
end

local function NewRound()
	for _, Player in pairs(game.Players:GetPlayers()) do
		local Index = #PlayersAlive + 1
		PlayersAlive[Index] = Player.Character or Player.CharacterAdded:Wait()
		
		Player.Character:WaitForChild('Humanoid').Died:Connect(function()
			PlayersAlive[Index] = nil
			if #PlayersAlive == 0 then
				RoundEnded()
			else
				print(#PlayersAlive..' people are alive!')
			end
		end)
	end
end

local function PickRandomCharacter()
	local CurrentlyAlive = #PlayersAlive
	local RandomNumber = math.random(1, CurrentlyAlive)


	local LoopCount = 0	
	for index, Char in pairs(PlayersAlive) do
		LoopCount = LoopCount + 1
		if LoopCount == RandomNumber then
			return Char
		end
	end
	
	return nil
end

NewRound()
print('Round started')
1 Like

While this thread has a solution already. I’m going to explain how in my current project the code stores data about the round. Especially whether or not a player has died.

Before the players get teleported I run a loop of all eligible players, which if they are able to participate, it creates a key in an array for them. Whenever a Humanoid that was participating in the round dies. An event fires the SetAlive function, which’ll label them as dead, and also logs their death time. (For rewards purposes)

local RoundConn = {}
local RoundPlayers = {}
for _, plr in pairs (Players:GetPlayers()) do
	if RoundPreparation:IsPlayerReady(plr) then
		local RootPart, Humanoid = RoundPreparation:Humroot(plr)
		
		RoundPlayers[plr.Name] = {
			Alive = true,
			TimeOfDeath = 0,
			InGame = false,
		}
	
		RoundPlayers[plr.Name].SetAlive = function(mode)
			RoundPlayers[plr.Name]["Alive"] = mode
			if mode == false then
				RoundPlayers[plr.Name]["TimeOfDeath"] = tick()
			end
		end
	end
end
1 Like