This code works just fine.
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)
But for some reason, when I define these variables below, the output tells me "Attempt to index nil with "Character."
plr = game.Players.LocalPlayer
humanoid = plr.Character:WaitForChild("Humanoid")
mouse = plr:GetMouse()
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)

3 comments
make sure its in a localscript. server doesn't have a localplayer because it's not a player, its a host
I don't want to copy and paste my answer that feels a bit dumb but someone else had a similar problem and my answer might solve it: https://scriptinghelpers.org/questions/102870/errorhumanioid-is-not-a-valid-member-of-model
doggy's answer is good too