Find the Mistake #2

I am trying to make whenever someone died it will print its name and “Died”. But that doesn’t work. This is a local script and it is child of StarterGui also there is no error in output.


wait(1)
hu = game.Players.LocalPlayer.CharacterAdded:Wait()
Jam = hu:WaitForChild("Humanoid")
Jam.Died:Connect(function(hat)
	print(hat .. "Died")
end)

Try:

local plr = game.Players.LocalPlayer;
local hu = plr.Character or plr.CharacterAdded:Wait()

Your code only listens for when the character is added

Died is not a valid member of Model.

That doesn’t work also why did you do like this:

hu = plr.Character or plr.CharacterAdded:Wait()

Avoid using wait at the beginning of your scripts, and because of this, the player’s character might’ve been added before you register the CharacterAdded event. You should also name your variables accordingly so you aren’t confused if you’re writing a long script. Humanoid.Died doesn’t return any parameters, so here’s the fix:

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()
local hum = char:WaitForChild("Humanoid")

hum.Died:Connect(function()
    print(player.Name .. " died")
end)
1 Like

Try to add in your script

if Jam.Health = 0 then
print(hat.." Died")

Instead of this make a local script in StarterCharacterScripts located in StarterScripts and put this inside it:

local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function() -- Event that will fire when the player joins the game
    local Player = game:GetService("Players").LocalPlayer or game.Players.PlayerAdded:Wait()
    local Character = Player.Character or Player.CharacterAdded:Wait()
    local Humanoid = Character:FindFirstChildOfClass("Humanoid")
    
    Humanoid.Died:Connect(function() -- Event listening for when the Humanoid dies
       print(Player, " just died")
    end
end)

What the above local script will do is when the player joins it will have an Event that when the Humanoid dies the code inside the Event will fire, we got the Humanoid from the variables that we have put inside the local script.

Another reason why we use the PlayerAdded Event is because the player can load in too fast that the game won’t even recongize the player but the PlayerAdded Event will wait for the player to join the game.

This should work perfectly fine but if there’s any errors then do not hesitate to tell me, if it works fine then do not forget to mark it as the solution so others know that this is the solution for this issue! :slightly_smiling_face:

The script should be in the ServerScriptService instead, because if that script is in every character, then it will print multiple times, depending on the amount of players in the game.

Consider putting your script somewhere else than StarterGui, maybe StarterCharacterScripts.

game.Players.LocalPlayer.Character:WaitForChild("Humanoid").Died:Connect(function()
    print (game.Players.LocalPlayer.Name.. " has died.")
end)

Here’s the code I use for setting up the character. Works really well:

local player = game.Players.LocalPlayer
local function Setup(char)
 local human = char:WaitForChild(“Humanoid”)
 human.Died:Connect(function()
  print(“ded”)
 end)
end
Setup(player.Character or player.CharacterAdded:Wait())
player.CharacterAdded:Connect(Setup)

Ignore formatting I’m on my phone rip