Having issues regarding the Died event for Humanoid

Hello everyone!

So I’m working on an item drop system that when the player dies, all the items on the person gets dropped on their dead body in the main workspace. However, for some reason the Died event isn’t firing, and I wanna know if maybe I did something wrong?

local playername = script.Parent.Parent.Name

repeat wait() until workspace[playername]

local workspaceplayer = game.Workspace[playername]
local lplayer = game.Players.LocalPlayer
local re = script.DropItem

workspaceplayer.Humanoid.Died:Connect(function() --this doesnt seem to fire

	re:FireServer(toolsinbackpack)

	print('player died, dropping any items')

end)

Thanks in advanced!

1 Like

Why are you using the died event on the player’s client? You can do it on the server by connecting a playeradded event and a characted added event.

game.Players.PlayerAdded:Connect(function(playerAdded)
  playerAdded.CharacterAdded:Connect(function(charAdded)
    charAdded:WaitForChild("Humanoid").Died:Connect(function()
      print("Hooray the humanoid is dead!")
    end)
  end)
end)

Why are you using the died event on the player’s client?

Probably because the devhub doesn’t say I can’t?

Is this event unable to work on client-side?

There can be a couple of reasons why it doesn’t work, here is some code you can try that may work

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

Humanoid.Died:Connect(function()
    re:FireServer(toolsinbackpack)
    print("player died; dropping items")
end)

As to reasons that your code may not work I’ll list here

  1. Your relying on the fact that no other models contain your character name, so there could be a object with your name and the script is trying to use that
  2. Your repeat loop is flawed, since if your trying to index a object that does not exist it will break the script and no longer execute, that why findfirstchild is a thing

Nevermind, got it to fixed. Forgot that I had another loop preventing it from running.

2 Likes

You’re firing to the server anyways so it saves the need to hook up a remote event.

You’re also passing the player’s backpack items to the server which can be spoofed and allow exploiters to pass whatever they want to be dropped and hence break your game, if you do this from the server then you have no need to run sanity checks and can ensure that the items in the player’s backpack are actually what they have.

If you end up using CharacterAdded & PlayerAdded on the client to connect to the Humanoid which is most likely what you’ll end up doing since you can’t rely on just grabbing the character using their name from workspace, then it will only fire for players who joined AFTER the client (and also not the client itself since the scripts are loaded after the player joins) Whereas the server is launched before any clients connect so you can guarantee it will get every player & not leave anyone out.

There are workarounds for all of these but it’s more practical and saves the effort for what you’re doing to just do it on the server.