My Humanoid.Died event sometimes doesn't fire?

The Scripting Support sub-category is intended for all development support relating to programming on the Roblox platform. This includes questions ranging in difficulty from extremely basic to even the most technical of issues.

You may present your thread how you choose, but you are required to answer the following questions in your thread;

  • What are you attempting to achieve? (Keep it simple and clear)
  • What is the issue? (Keep it simple and clear - Include screenshots/videos/GIFs if possible)
  • What solutions have you tried so far? (Have you searched for solutions through the Roblox Wiki yet?)

You may then include any further details.

What are you attempting to achieve?
I have a system which is supposed to set a BoolValue under the player to false when a player’s Humanoid dies. The way I do this is by adding a BoolValue called “Surviving” to every player that joins under a players.PlayerAdded event, which works properly. Inside this event connected function is another function, which is connected to the Humanoid.Died as follows, “Humanoid.Died:Connect(function()”, where “Humanoid” is found using :WaitForChild() on the player’s character. When this event fires the function should run, setting the “Surviving” BoolValue’s value to false.

What is the issue?
The issue is that sometimes the Humanoid.Died event doesn’t fire. In some cases it works, though sometimes I’ve seen that even after dying a player is considered surviving.

What solutions have you tried so far?
I haven’t really, I’m quite clueless as to why this might be happening. I will edit the topic as I find new clues.

That’s about it, thank you for reading. Sorry if I have not formatted my text properly and all as this is my first DevForum topic.

Edit:
Here’s the basic structure of my script.

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local surviving = Instance.new("BoolValue")
	surviving.Parent = player
	surviving.Value = true
	surviving.Name = "Surviving"
	local character
	local humanoid
	--Other things are here, as I am using this in the same script as my DataStores.
	player.CharacterAdded:Connect(function()
		character = player.Character
		humanoid = character:WaitForChild("Humanoid")
		--Other things again, this time related to an overhead BillboardGui.
	end)
	repeat wait() until humanoid ~= nil
	print("Humanoid found!") --This always prints, I have verified.
	humanoid.Died:Connect(function()
		surviving.Value = false --The statement which should set the value to false.
	end)
end)
1 Like

Mind posting your code around where the died event is? Also you don’t have to copy paste the questions! Those are just a guideline. :smile:

1 Like

Oh alright, I’ll do that. Thank you! Though could you tell me how I add a code block?

1 Like

Use:
```lua

```

Where the ` symbol is that to the left of the 1 on the qwerty keyboard, not an apostrophe.

2 Likes

So it’s the same as Discord, thanks!

From what you first posted (I’m not sure because I havent seen your code), you didn’t make any mention of the CharacterAdded event.

That code will work the first time the Character loads in (as Humanoid changes whenever a new Character is spawned in), however afterwards, you will have a new Character and therefore a new Humanoid.

Inside your PlayerAdded event, you should use:

Player.CharacterAdded:Connect(function(Character)

end)
1 Like

Try this!:wink:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local surviving = Instance.new("BoolValue")
	surviving.Parent = player
	surviving.Value = true
	surviving.Name = "Surviving"
	local character
	local humanoid
	--Other things are here, as I am using this in the same script as my DataStores.
	player.CharacterAdded:Connect(function()
		local character = player.Character
		local humanoid = character:WaitForChild("Humanoid")
		--Other things again, this time related to an overhead BillboardGui.
		humanoid.Died:Connect(function()
			print(humanoid.Parent.Name .. " died")
			surviving.Value = false --The statement which should set the value to false.
		end)
	end)
end)

EDIT: You should see a message in the output whenever you die.

2 Likes

Alright I’ll try it, thanks!

Didn’t seem to make a difference, failure rate is still the same.

Are you setting the value of surviving to true on the server?

Yes, all of this is handled on the server.

This line of code serves no purpose.

Other than that, your code hooking on humanoid.Died is outside the Player.CharacterAdded listener, so it will only run for one humanoid until the player respawns.

7 Likes

Firstly, I would use the Variable that is given to you in the CharacterAdded Function

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
	local surviving = Instance.new("BoolValue")
	surviving.Parent = player
	surviving.Name = "Surviving"
    surviving.Value = true
	--Other things are here, as I am using this in the same script as my DataStores.
	player.CharacterAdded:Connect(function(character)
	local humanoid = character:WaitForChild("Humanoid")
	print("Humanoid found!")
		--Other things again, this time related to an overhead BillboardGui.
		if humanoid then
		humanoid.Died:Connect(function()
        print(tostring(player) ..  'has died. ')
		surviving.Value = false --The statement which should set the value to false.
			end)
		end
	end)
end)

Try this. It is a bit redundant checking if Humanoid is not equal to nil.

2 Likes

Yeah I removed that line, I’ll try with putting the humanoid.Died inside the player.CharacterAdded

I’d try again, it’s likely you’re missing something obvious here. TJ101’s code, for the most part, should work.

Yeah I don’t know really, I wrote that a while back and have improved since. I’ll give it a thorough review.

Thanks! I removed the redundant line and put the humanoid.Died event inside the player.CharacterAdded event and now it seems to be working with a 0% failure rate.