Event on death not firing

Hello, i’v some trouble with this script.

  1. The goal is;
    When a player die, if there is a child named “CswordDeath” on his character, it have to run an event

  2. What’s the issue;
    Even with the child needed, it’s not working well. (It’s just running the elseif event for whatever reason)

  3. What things I already tried;
    I tried many different way to do it but that was the only way I thought of to make it work. (The whole idea is, when I kill someone with a specific sword, it create a child on his character, and this script have to detect it to run an event)


  • This is a script in Server Script Service.

  • (“if character:WaitForChild(“CswordDeath”) then” is a WaitForChild because “CswordDeath” is supposed to be created when the player die, so if I just put FindFirstChild it wouldn’t find anything because it creates it and search for it in the same time)

game:GetService('Players').PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		character:WaitForChild("Humanoid").Died:Connect(function()
			print("Player "..player.Name.." has died")
			if character:WaitForChild("CswordDeath") then
				local hum = character:FindFirstChild("Humanoid")
				hum.BreakJointsOnDeath = false
				for i, v in pairs(character:GetChildren()) do
					if v:IsA("Part") then
						v.Anchored = true
						v.CanCollide = false
						v.Color = Color3.fromRGB(0,0,0)
					else if v:IsA("Shirt") or v:IsA("ShirtGraphic") or v:IsA("Pants") or v:IsA("Decal") or v:IsA("Accessory") then
							v:Destroy()
						end		
					end
				end
			end
		end)
	end)
end)

Thanks in advance, have a good day.

1 Like

Try checking for “BasePart” rather than just “Part”. As the character may contain MeshParts

1 Like

I dont think adding WaitForChild is a good idea. You have to add FindFirstChild when you are making an if statement, its a better way of checking if any child exists or not, and will not give you any errors. WaitForChild in if statements may not work, and may give you an error. I have never tried adding a WaitForChild in an if statement, but I dont think this is a good way way.

You can make a variable of getting the specific child with FindFirstChild, and then you just mention the variable name in the if statement.

See if this code does any better:

local Players = game:GetService("Players")

Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local humanoid = character:WaitForChild("Humanoid")

		humanoid.Died:Connect(function()
			print("Player "..player.Name.." has died")
			local CswordDeath = character:FindFirstChild("CswordDeath")

			if CswordDeath then
				humanoid.BreakJointsOnDeath = false

				for i, v in pairs(character:GetChildren()) do
					if v:IsA("BasePart") then
						v.Anchored = true
						v.CanCollide = false
						v.Color = Color3.fromRGB(0,0,0)

					elseif 
						v:IsA("Shirt") or 
							v:IsA("ShirtGraphic") or 
							v:IsA("Pants") or
							v:IsA("Decal") or 
							v:IsA("Accessory") 
					then
						v:Destroy()
					end
				end
			end
		end)
	end)
end)
1 Like

Hi, thanks for responding.
The tictac67’s suggestion was actually the good one, I tried with you’r solution but it didn’t really work for whatever reason, and it was also more complicated because I would have to add a while loop and some other stuffs.
But, I also had to put the BreakJointsOnDeath to false before checking if there was a CswordDeath child, and add a “else”, to put back BreakJoinsOnDeath to true to not make anymore problems if I kill someone but with another sword, but now it work!
Thanks anyway, have a good day.