Humanoid.Died not working

Hello! My script doesn’t work and I need help. In my script, Humanoid.Died doesn’t seem to work but doesn’t give an error. The script looks something like this:

local players = game:GetService("Players")

script.Parent.ClickDetector.MouseClick:Connect(function()
    local playersInGame = {}

    for i, player in pairs(players:GetPlayers() do
        table.insert(playersInGame, player)
        player.Character.Humanoid.Died:Connect(function() --here's the line that doesn't work
            print(player.Name.. "died")
            table.remove(playersInGame, table.find(playersInGame, player)
        end)
    end

    wait(5)

    print("round ended")
    for i, player in pairs(playersInGame) do
        player.leaderstats.Points.Value += 5
    end
end)

Any kind of help is appreciated :grin:
(sorry if the post was badly formatted, it’s my first time posting)

I believe the event only works on server script. You could try to test if the event gets fired in a local script and a server script.

The script was written in a server script.

Sorry, I meant script.Parent.ClickDetector.MouseClick instead of script.Parent.MouseClick

I have corrected the script (probably) and the below script should work. I have also listed the changes and feedback.

  • You were missing a parenthesis to close the loop constructor. I also replaced i with _ as you don’t use the i variable and it’s good practice.

  • The event is firing, just that .Character isn’t there yet so you need to wait using :WaitForChild().

  • You were also missing another closing parenthesis on the table.remove so I added that.

  • wait() is deprecated and shouldn’t be used so I went ahead and changed it to task.wait() for you. Thank me later.

local players = game:GetService("Players")

script.Parent.MouseClick:Connect(function()
    local playersInGame = {}

    for _, player in pairs(players:GetPlayers()) do -- Added parenthesis and substituted i
        table.insert(playersInGame, player) 
        player: WaitForChild("Character").Humanoid.Died:Connect(function() -- Wait for the character. 
            print(player.Name.. "died")
            table.remove(playersInGame, table.find(playersInGame, player)) -- Another parenthesis missing
        end)
    end

    task.wait(5) -- wait() is deprecated 

    print("round ended")
    for _, player in pairs(playersInGame) do -- No need for i 
        player.leaderstats.Points.Value += 5
    end
end)

This script might work but I’m not sure and haven’t tested it. I just wrote this from the back of my head.

1 Like

It gave me a warning: Infinite yield possible on Players.waffelpi:WaitForChild(“Character”)

I think it’s because “Character” isn’t a child of a player.

try to use

workspace:WaitForChild(player.Name):FindFirstChildOfClass("Humanoid").Died:Connect(function()
--your code
end)
1 Like

Pretty sure it’d be the same as using .Character but I tried it and still it doesn’t :confused:

Check where you have the script and the clickdetector, everything works fine for me

If the script is under a clickdetector then use script.Parent, if it is on par with part then use script.Parent.Clickdetector

1 Like
local players = game:GetService("Players")

script.Parent.ClickDetector.MouseClick:Connect(function()
    local playersInGame = {}

    for _, player in pairs(players:GetPlayers()) do
        table.insert(playersInGame, player)
        player.Character.Humanoid.Died:Connect(function() --here's the line that doesn't work
            print(`{player} died`)
            table.remove(playersInGame, table.find(playersInGame, player))
        end)
    end

    task.wait(5)

    print("round ended")
    for _, player in pairs(playersInGame) do
        player.leaderstats.Points.Value += 5
    end
end)

you can also use HealthChanged and see if that will help further

here how:

hum.HealthChanged:Connect(function(newhealth)
if newhealth == 0 then
print(“DIED”)
end end)

I tried using the script I wrote for this post and yeah, it did work?? I’ll try rewriting the actual script :sweat_smile:

The problem was actually because in the actual script, there was a line that disables the script which prevented the event to happen. I appreciate all the help. Sorry for wasting your time if I did.

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