Detect when death

Hello, i would like some help in this script i wrote. It works but i want for it to detect when the Hit.parent is dead. If so, it changes the team of the player who got the sword.

The if Humanoid.Died line doesn’t work and i would like help


local Turn = 1
local Damage = 12
local Cooldown = 1


script.Parent.Activated:Connect(function()
	if script.Parent.Cooldown.Value == false then
		script.Parent.Cooldown.Value = true
		script.Parent.CanDamage.Value = true

		local Humanoid = script.Parent.Parent.Humanoid
		local Anim1 = Humanoid:LoadAnimation(script.Parent.Attack1)
		local Anim2 = Humanoid:LoadAnimation(script.Parent.Attack2)
		script.Parent.Sound:Play()


		if Turn == 1 then
			Anim1:Play()
			Turn = 2


			Anim1.Stopped:wait(Cooldown)
			script.Parent.CanDamage.Value = false
			script.Parent.Cooldown.Value = false

		elseif Turn == 2 then
			Anim2:Play()
			Turn = 1

			Anim2.Stopped:Wait(Cooldown)
			script.Parent.CanDamage.Value = false
			script.Parent.Cooldown.Value = false
		end
	else
		return
	end
end)

script.Parent.Handle.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Humanoid = Hit.Parent.Humanoid

		if script.Parent.CanDamage.Value == true then
			Humanoid:TakeDamage(Damage)
			script.Parent.Swing:Play()
			script.Parent.sword.ParticleEmitter:Emit(1)
			script.Parent.CanDamage.Value = false
			
			if Humanoid.Died then
				print("Death")
			end
			
		else
			return
		end
	else
		return
	end


end)	




1 Like

Try using the health of the character:

Humanoid.Health <= 0

2 Likes

I already tried but it didn’t worked

1 Like

Did you connected Humanoid.Died event to the print? I think this is why the code is uncompleted. It must’ve be done like Humanoid.Died:Connect(function().

1 Like

I tried this block of your code and it worked fine:

	if Hit.Parent:FindFirstChild("Humanoid") then
		local Humanoid = Hit.Parent.Humanoid

		if script.Parent.CanDamage.Value == true then
			Humanoid:TakeDamage(Damage)
			script.Parent.Swing:Play()
			script.Parent.sword.ParticleEmitter:Emit(1)
			script.Parent.CanDamage.Value = false

			if Humanoid.Died then
				print(Hit.Parent.Name.." Died")
			end
		else
			return
		end
	else
		return
	end

Maybe it is because i tried the code on a dummy?

1 Like
local Turn = 1
local Damage = 12
local Cooldown = 1


script.Parent.Activated:Connect(function()
	if script.Parent.Cooldown.Value == false then
		script.Parent.Cooldown.Value = true
		script.Parent.CanDamage.Value = true

		local Humanoid = script.Parent.Parent.Humanoid
		local Anim1 = Humanoid:LoadAnimation(script.Parent.Attack1)
		local Anim2 = Humanoid:LoadAnimation(script.Parent.Attack2)
		script.Parent.Sound:Play()


		if Turn == 1 then
			Anim1:Play()
			Turn = 2


			Anim1.Stopped:wait(Cooldown)
			script.Parent.CanDamage.Value = false
			script.Parent.Cooldown.Value = false

		elseif Turn == 2 then
			Anim2:Play()
			Turn = 1

			Anim2.Stopped:Wait(Cooldown)
			script.Parent.CanDamage.Value = false
			script.Parent.Cooldown.Value = false
		end
	else
		return
	end
end)

script.Parent.Handle.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Humanoid = Hit.Parent.Humanoid

		if script.Parent.CanDamage.Value == true then
			Humanoid:TakeDamage(Damage)
			script.Parent.Swing:Play()
			script.Parent.sword.ParticleEmitter:Emit(1)
			script.Parent.CanDamage.Value = false
			
			-- Check if the Hit.Parent has died
			Humanoid.Died:Connect(function()
				local player = game.Players:GetPlayerFromCharacter(Hit.Parent)
				if player then
					local team = player.TeamColor
					-- Change the player's team to a new team
					if team == BrickColor.new("Bright red") then
						player.TeamColor = BrickColor.new("Bright blue")
					else
						player.TeamColor = BrickColor.new("Bright red")
					end
				end
			end)
		else
			return
		end
	else
		return
	end


end)	
´´´

I created a block with your code inside of it and walked into the block.

It worked fine.

1 Like

Humanoid.Died is a connection not a statement. You need to use Health.

I think you misunderstood, i meant that if a player gets killed by the sword then the player with the sword team changes

You would need to use the Died moment to trigger a change to the Player holding the sword.

I was only saying that your script works fine for knowing if the Hit died.

Call another function if Died as in:

	if Humanoid.Died then
		changeTeam() -- call function to change the team of the player holding the sword
	end
1 Like

im not sure how to do that xd
but you need to add a thing in sword to detect if player dies

1 Like

This is a connection not a property so it wouldn’t work.

Try this; I used “GetPropertyChangedSignal” Instead. It waits for a specific property to be changed.

local Turn = 1
local Damage = 12
local Cooldown = 1


script.Parent.Activated:Connect(function()
	if script.Parent.Cooldown.Value == false then
		script.Parent.Cooldown.Value = true
		script.Parent.CanDamage.Value = true

		local Humanoid = script.Parent.Parent.Humanoid
		local Anim1 = Humanoid:LoadAnimation(script.Parent.Attack1)
		local Anim2 = Humanoid:LoadAnimation(script.Parent.Attack2)
		script.Parent.Sound:Play()


		if Turn == 1 then
			Anim1:Play()
			Turn = 2


			Anim1.Stopped:wait(Cooldown)
			script.Parent.CanDamage.Value = false
			script.Parent.Cooldown.Value = false

		elseif Turn == 2 then
			Anim2:Play()
			Turn = 1

			Anim2.Stopped:Wait(Cooldown)
			script.Parent.CanDamage.Value = false
			script.Parent.Cooldown.Value = false
		end
	else
		return
	end
end)

script.Parent.Handle.Touched:Connect(function(Hit)
	if Hit.Parent:FindFirstChild("Humanoid") then
		local Humanoid = Hit.Parent.Humanoid

		if script.Parent.CanDamage.Value == true then
			Humanoid:TakeDamage(Damage)
			script.Parent.Swing:Play()
			script.Parent.sword.ParticleEmitter:Emit(1)
			script.Parent.CanDamage.Value = false

			Humanoid:GetPropertyChangedSingnal("Health"):Connect(function(Health)
				if Health <= 0 then
				print("Death")
			end
end)
		else
			return
		end
	else
		return
	end


end)
1 Like

It doesn’t prints “death”. Its okay guys

Does the humanoid take damage does the swing sound effect play?

1 Like

Yes, the swing sound plays at the same time that the humanoid gets hit. Even if it doesnt get hit it still plays it

you dont put Humanoid.Died in if statement
you need to connect Died with :Connect like

Humanoid.Died:Connect(function()
end)
1 Like