Knife stops working after hitting a part

I started this knife tool tonight with no assistance from others in the process. I learned what
if p.Parent:FindFirstChild("Humanoid") then is and I wanted to see if I could make a tool. The way I wanted to test this was an admin tool that only my friends could use to have fun. It works fine when killing players, but whenever they click and the knife hits a part it will break the killing effect on others. The issue is shown in this video here:

Here is the code seeing as the fact the culprit is it:

-- Animations --
local R15Idle = script.Parent.Animations:WaitForChild("R15Idle")
local EquipAnim = script.Parent.Animations:WaitForChild("EquipAnim")
local AttackAnim = script.Parent.Animations:WaitForChild("AttackAnim")
-- Sounds --
local Hit = script.Parent.Handle.Hit
local Equip = script.Parent.Handle.Equip
local Unequip = script.Parent.Handle.Unequip
local Swing = script.Parent.Handle.Attack
-- Core Variables --
local debounce = false
local IsAttacking = false
local HitDelay = false
local dmgValue = 100
local BloodEffects = script.Parent.Handle:GetChildren()
-- Main Script --

script.Parent.Equipped:Connect(function()
	R15IdleAnim = script.Parent.Parent.Humanoid:WaitForChild("Animator"):LoadAnimation(R15Idle)
	EquipAnimation = script.Parent.Parent.Humanoid:WaitForChild("Animator"):LoadAnimation(EquipAnim)
	AttackAnimation = script.Parent.Parent.Humanoid:WaitForChild("Animator"):LoadAnimation(AttackAnim)
	
	R15IdleAnim:Play()
	EquipAnimation:Play()
	Equip:Play()
end)

script.Parent.Activated:Connect(function()
	if not debounce then
		debounce = true
		
		AttackAnimation:Play()
		Swing:Play()
		IsAttacking = true
		
		wait(0.5) -- if issues occur, set back to 1.
		debounce = false
		IsAttacking = false
	end
end)

script.Parent.Unequipped:Connect(function()
	Unequip:Play()
	
	R15IdleAnim:Stop()
	AttackAnimation:Stop()
	EquipAnimation:Stop()
	Hit:Stop()
	Swing:Stop()
	Equip:Stop()
end)

script.Parent.Handle.Touched:Connect(function(p)
	if not IsAttacking then
		-- do nothing
	else
		if not HitDelay then
			HitDelay = true
			if p.Parent:IsA("BasePart") or p.Parent:IsA("Accoutrement") then
				-- do nothing
			else
				if p.Parent:FindFirstChild("Humanoid") then
					p.Parent.Humanoid.Health = p.Parent.Humanoid.Health - dmgValue

					for i, v in pairs(BloodEffects) do
						if v:IsA("Decal") then
							v.Transparency = 0
						end
					end

					Hit:Play()

					wait(1)
					HitDelay = false
				end
			end
		end
	end
end)

After HitDelay is set to true, your if statement checks to see if there is a Humanoid associated with whatever part was touched. If there is, it does some stuff and then sets HitDelay back to false. Notice that if the if statement is false, there’s nothing setting HitDelay back to false so that it can be executed again.

I’d move the HitDelay = true line to within the if statement that checks for a Humanoid. That should solve your issue.

1 Like

I will attempt this fix. I’ll report back if any further issues occur.

Thank you. I didn’t even think about that part. I thought of it as something very insignificant and I thought it was something about the actual if p.Parent:FindFirstChild("Humanoid" then that was causing the issue. Much appreciated.