Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would I detect if a player has died?

Asked by 3 years ago

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.

2 answers

Log in to vote
1
Answered by 3 years ago
Edited 3 years ago

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
0
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? flamethower2002 50 — 3y
0
you can also disconnect the event by saving the connection in a table, I'll edit my answer to do that second_120 5 — 3y
0
Also to answer your question, yes you should be able to use that to remove players from a table second_120 5 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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.

0
There is syntax error at my code 3rd line Sabailuridze 126 — 3y
0
it should be if character:WaitForChild("Humanoid").Health == 0 then Sabailuridze 126 — 3y

Answer this question