How check if player in table is dead?

Hello
I need help checking if a player is dead and removing him from the table

Script:

local TableArena = {}
local TablePlayers = {}
local Events = require(script.Events)

function PlayerAdded(Player)
	table.insert(TablePlayers, Player)
	print(TablePlayers)
end

game.Players.PlayerAdded:Connect(PlayerAdded)

while true do
	repeat
		wait(1)
	until table.maxn(TablePlayers) >=2

	wait(5)
	Events.GameStart(TablePlayers, TableArena)
	
	repeat
		Events.RandomEvent(TableArena)
		wait(20)
	until table.maxn(TableArena) == 1
	
end

Module Script:

local Events = {}

function Events.GameStart(TablePlayer, TableArena)
	for _,v in pairs(TablePlayer) do
		table.insert(TableArena, v)
		v.Character.HumanoidRootPart.CFrame = workspace.Arena.Tp.CFrame
	end
end
return Events

I want to remove a player from the table if he dies if he is in the table “TableArena”
All the ways I did didn’t work

1 Like
if Player.Character:FindFirstChildWhichIsA("Humanoid").Health == 0 then
end
2 Likes

No, you should not check if the Health is 0 as @Rekotora3349 suggested. There is an event that fires when a Humanoid dies Humanoid | Documentation - Roblox Creator Hub

Use that event to search in your table and remove the player from it

2 Likes
local TableArena = {}
local TablePlayers = {}
local Events = require(script.Events)

function PlayerAdded(Player)
	table.insert(TablePlayers, Player)
	print(TablePlayers)
end

game.Players.PlayerAdded:Connect(PlayerAdded)

while true do
	repeat
		wait(1)
	until table.maxn(TablePlayers) >= 2

	wait(5)
	Events.GameStart(TablePlayers, TableArena)

	for _, player in TableArena do --loop over all the players in TableArena
		local humanoid = player.Character:FindFirstChild("Humanoid") --get the humanoid
		humanoid.Died:Once(function() --when player dies execute this function
			table.remove(TableArena, table.find(TableArena, player)) --find the position of the dead player and remove it from the the TableArena table.
		end)
	end

	repeat
		Events.RandomEvent(TableArena)
		wait(20)
	until table.maxn(TableArena) == 1
end

3 Likes

Thank you, everything works fine

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.