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.
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.