I need to figure out if a player dies during a round so I can exclude them from a winners roster - what would the best way to go about this be? Yes, I know about Humanoid.Died, but as far as I'm aware that only detects if a player just died. I assume I would need to fire a function using Humanoid.Died and add/remove a player to a table when that happens.
COMMUNITY QUESTION
How would I detect if a player has died?
2 answers
Humanoid.Died doesn't actually pass anything so in order to reference the player you'll need to do something like this:
game:GetService('Players').PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
character:WaitForChild("Humanoid").Died:Connect(function()
print(player.Name .. " has died!")
end)
end)
end)
If that doesn't work for you, you can also used Player.CharacterRemoving but this event doesn't fire the instant the player dies and actually fires when all the player's bits disappear
player.CharacterRemoving:Connect(function(character)
-- if you can't just use the player variable outside this function then you need to get it from the character
local player = Players.GetPlayerFromCharacter(character)
print(player.Name .. " has died!")
end)
Here's an edit to also be able to disconnect the function
local characterRemovingEvents = {}
function Start()
local characterRemovingEvent = player.CharacterRemoving:Connect(function(character)
-- if you can't just use the player variable outside this function then you need to get it from the character
local player = Players.GetPlayerFromCharacter(character)
print(player.Name .. " has died!")
characterRemovingEvents[player]:Disconnect() -- if you want to disconnect when the player dies
characterRemovingEvents[player] = nil -- clean up
end)
characterRemovingEvents[player] = characterRemovingEvent
end
function Stop()
-- we will now remove the rest of the connections
for _, connection in pairs(characterRemovingEvents) do
connection:Disconnect()
end
characterRemovingEvents = {} -- clean up
end
Maybe check if player's health has come down to 0
game:GetService('Players').PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
if character:WaitForChild("Humanoid").Health = 0 then
print(player.Name .. " has died!")
end
end)
end)
But I do not know if this is best choice or not.
2 comments
0
There is syntax error at my code 3rd line
0
it should be if character:WaitForChild("Humanoid").Health == 0 then
This discussion is preserved as it was. New answers, comments, and votes are closed.

3 comments
If I'm correct - this would detect if a player dies but not if they have died at some point. Would I be able to create a function that removes players from a table when they die with the string you suggested?
you can also disconnect the event by saving the connection in a table, I'll edit my answer to do that
Also to answer your question, yes you should be able to use that to remove players from a table