How to check if a specific player from a table died?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to learn how to do this.
  2. What is the issue? Include screenshots / videos if possible!
    The issue is i dont know how to do this
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I looked on the devforum, youtube and nothing worked.
    Im trying to make a murder mystery game in which, it is really important to check if everyone is dead, but not the murderer, or to check if a sheriff is dead so you can give it to another player, and to check if the murderer is dead.
2 Likes

Assuming PlayerTable is a table containing all the players alive at the beginning of the round.


local DeadPlayers = {};

for _,v in ipairs(PlayerTable) do
	if v.Character then
		if v.Character:FindFirstChild("Humanoid") then
			v.Character.Humanoid.Died:Connect(function()
				table.insert(DeadPlayers,v);
			end)
		end
	end
end
1 Like

You can use table.find() to find the position of an item in a table, or nil if there is nothing matching in the table. In your case, you could store the sheriff and murderer in variables somewhere, and then keep track of who is alive in a table. Whenever someone dies, use table.find to check if the sheriff and the murderer are still alive:

local sheriff, murderer = game.Players.Player1, game.Players.Player2 
-- store your special players here
local AlivePlayers = {} -- A list that should contain all living players. Use table.remove to remove players when they die

if table.find(AlivePlayers, sheriff) and not table.find(AlivePlayers, murderer) then
 print('Murderer is dead, sheriff is alive')
 -- End this round, innocents win
elseif not table.find(AlivePlayers, sheriff) and table.find(AlivePlayers, murderer) then
 print('Sheriff is dead, murderer is alive, assign new sheriff')
 -- Assign a new sheriff
else
 -- The sheriff and the murderer are still alive. Game continues as normal
end

How to Get Players Alive / Dead


First you want to have your player table. I assume you already have this, based on the way you worded your post, but for example I will include it in the code bit.

In this example it will include all the players that are currently in the game.

local PlayerList = {} -- Table containing the players.

for _, Player in pairs(game:GetPlayers()) do
	table.insert(PlayerList,Player) -- Adds each player to the table.
end

After you have your player list, you can (optionally) create a new table to store dead players. This is not required for every case, but can be useful for many.

In order to set this up, you must change the tables of the player when they die.

local PlayerList = {}
local DeadPlayers = {} -- Table containing dead players.

for _, Player in pairs(game:GetPlayers()) do
	local Character = Player.Character
	
	if Character then
		table.insert(PlayerList,Player)
		
		Character:FindFirstChild("Humanoid").Died:Connect(function()
			table.insert(DeadPlayers,Player) -- Adds player to dead players.
			table.remove(PlayerList,table.find(PlayerList,Player)) -- Removes player from alive players.
		end)
	end
end

If you want further accessibility, I would recommend creating a BindableEvent called “PlayerDied” that fires within the Humanoid.Died connection. You could connect its event to end the round if the player that died is the murderer, sheriff, or if there are no remaining opponents.


I really hope this helps you in what you’re trying to accomplish. Have a good day, and good luck to you.

  • Galactiq