How can i make it so it requires a tool to be fired for a function to work?

Now, I have this melee tool, and once the tool touches a player it breaks a joint. Issue is, I’d like to make it so the animation must be playing for it to work, else it won’t. Anyone knows a good way to achieve this?

Here is the code

local tool = script.Parent

local canDamage = false

local sound = script.Parent["Hit"]

local dmg = script.Parent.Damage.Value

local animation = script.Parent.Animation

local RESET_TIME = 5


tool.DMG.Touched:Connect(function(otherPart)

	local humanoid = otherPart.Parent:FindFirstChild("Humanoid")
	if not humanoid then 
		return 
	end

	if humanoid.Parent ~= tool.Parent and canDamage then 

		humanoid:TakeDamage(dmg)
		
		otherPart:FindFirstChild("Head").Neck:Destroy()
		otherPart:FindFirstChild("Head").CanCollide = true

	else

		return

	end



	canDamage = false
end)



tool.Activated:Connect(function(slash)

	if not tool:GetAttribute("Activated") then

		tool:SetAttribute("Activated", true)  -- Set attribute to true

		local str = Instance.new("StringValue")

		str.Name = "toolanim"

		str.Value = "Slash" 

		str.Parent = tool

		canDamage = true

		track = script.Parent.Parent.Humanoid:LoadAnimation(animation)
		track:Play()
		sound:Play()
		

		task.wait(RESET_TIME)  -- Wait for reset duration

		tool:SetAttribute("Activated", false)  -- Reset attribute
	end


end)

Tool hierarchy:

Tool

Hit
Attack
Damage
DMG
Handle
Animation

1 Like