Humanoid.Died Isn't Working

I want to remove a player from the game if they die, so I put Humanoid.Died:Connect(function() But that isn’t working. Here is a screenshot:

image

There are other lines of code as well. Do I need to make a while loop? Thanks.

6 Likes

make sure you check correctly if the Player has a Humanoid, or has a Character

local Array = game.Players:GetPlayers()
for i,p in Array do
    if p.Character then -- IF Player has a Character (Might not be useful)
        local Hum = p.Character:FindFirstChildOfClass("Humanoid") -- Finds the First Humanoid
        if Hum then -- If here is a Humanoid
            Hum.Died:Connect(function()
                print"Yes" -- prints
            end)
        end
    end
end

The script should work, even with yours, if it doesnt, its likely something preventing the code from running, so i recommend debugging

print"Step 1"
-- next process
print"Step 2"
-- next process
print"Step 3"
-- next process

A while loop would just end up creating new connections per iteration, so much so that it would crash depending on how long it ran, a for loop is a loop that runs depending on how long it is given, you can give it a number so it could run for that long, or a table so it can run for the items inside

3 Likes

Isn’t working… :frowning_with_open_mouth:

aaaaaaaaaaa

3 Likes
1 Like

@DasKairo is right. You should definitely check if the player has a character or if the player’s character has a Humanoid first.

Are you checking for when players join the game? Because, from what I see, your code is just checking the current players in the game, which would be zero players since you have not joined, yet.

Maybe this will help:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        character:WaitForChild("Humanoid").Died:Connect(function()
            player:Kick("You died.")
        end)
    end)
end)

Or

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
    local character

    repeat 
        character = player.Character
    until character

    character:WaitForChild("Humanoid").Died:Connect(function()
        player:Kick("You died.")
    end)
end)
2 Likes

The for loop is not running at all…

1 Like

can you show us the code above this for loop?

1 Like

yea but code after it is still running

2 Likes

Is that all?

But one thing: Why are you creating that many DataStores?
There is no reason to have that many DataStores in your game, you should Instead have only ONE DataStore, and save a Table of Data, its way more efficient than having multiple.

1 Like

eh im dumb in datastores I focus more on logical stuff and yes that is all

1 Like

Yeah, then @Ovibion is likely correct, you are probably running the for loop very early, you should probably wait until there is a Player, or do what He did where you create a Died Event when the Player’s Character is added.

1 Like

Its not detecting the if char statement

1 Like

This thread is becoming ridiculous at this point. You should be starting off with this code:

There’re already several issues with this code. First of all, the Humanoid.Died event only fires with one character, and it’s done, and it does not work until they rejoin. I’ll use this code as a base since it is easy to work off of to form my examples.

So, the first issue that you’re experiencing is that Humanoid.Died does not work on the server. This event only works on the client or network owner of the humanoid instance.

So, in your case, how would you accomplish this? Well, you might not even need to tell if the character died in the first place if you can listen for CharacterAdded.

Another option is to use RemoteEvents, which is insecure.

The option I recommend is to wait for the players’ Neck joint to be destroyed. We can do this with ChildRemoved or AncestryChanged.


To do this, we first need to define the Players service.

local Players = game:GetService('Players')

Then, we need to define a table array:

local connections = {}

This is quite simple so far. Next, we need to add a function for PlayerAdded:

function onPlayerAdded(Player)
	--Perform any other data functions here
end

We will then create a connection variable and connection to CharacterAdded:

local connection = Player.CharacterAdded:Connect(function(Character)

end)

we will get back to this later.
Inside of here, we can put multiple things. I have some code that searches for the neck, or, if not found, the root joint:

local Neck = Character:FindFirstChild("Neck", true) or Character:FindFirstChild("Root", true) or Character:FindFirstChild("Root Hip", true)
if not Neck then
	Player:Kick("hacker.")
	return
end

I also kick them if they don’t have a neck (because if they don’t, they probably are removing it to create inappropriate animations and exploit anyways).

Now, we need to check if the neck is removed. We can do this by listening for the ChildRemoved event of the parent:

Neck.Parent.ChildRemoved:Connect(function(c)
	if c == Neck then
		print('the player died')
	end
end)

If the child that was removed is the neck, then the player probably died since their joints broke. We can then assume the player died.

Now, we need to wrap up the script by inserting the connection into the table:

connections[Player] = connection

Now, we’re done with the PlayerAdded function! Now, we’re going to move on to this:

function onPlayerRemoving(Player)
	connections[Player]:Disconnect()
end

This simply will disconnect the connection made earlier to prevent memory leaks.

We also need to connect up our functions:

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

Now, we’re basically done with the whole thing. Here’s the full script if you are too lazy to follow this tutorial:

local Players = game:GetService("Players")
local connections = {}

function onPlayerAdded(Player)
	--Perform any other data functions here
	local connection = Player.CharacterAdded:Connect(function(Character)
		local Neck = Character:FindFirstChild("Neck", true) or Character:FindFirstChild("Root", true) or Character:FindFirstChild("Root Hip", true)
		if not Neck then
			Player:Kick("hacker.")
			return
		end
		Neck.Parent.ChildRemoved:Connect(function(c)
			if c == Neck then
				print('the player died')
			end
		end)
	end)
	connections[Player] = connection
end

function onPlayerRemoving(Player)
	connections[Player]:Disconnect()
end

Players.PlayerAdded:Connect(onPlayerAdded)
Players.PlayerRemoving:Connect(onPlayerRemoving)

I’ve tested it and it works fine for me.

2 Likes