Help on tool debounce

Hey guys, I’m trying to make a sword combat system but I have a problem with animation debounce.

-- Settings

local CooldownTime = 1
local Damage = 30

-- Services 

local Players = game:GetService("Players")
local ServerStorage = game:GetService("ServerStorage")

-- Tool Variables 

local tool = script.Parent
local swordBlade = tool.Handle

-- Animations, Humanoid, Player 

local humanoid, attack1, attack2, attack3, attack4, attack5, heavySlice1, heavySlice2, equip, player

-- Bool Values

local canDamage = true
local isAttacking = false

-- Equip Function
local function onEquip()
	-- Set humanoid, animations and player variables
	local char = tool.Parent
	humanoid = char:WaitForChild("Humanoid")
	player = Players:GetPlayerFromCharacter(char)
	equip = humanoid:LoadAnimation(tool:WaitForChild("Equip"))
	equip:Play()
end

-- Detect Hit Function 

local function onDetectHit(otherPart)
	local partParent = otherPart.Parent
	local otherHumanoid = partParent:FindFirstChildWhichIsA("Humanoid")
	-- Make sure blade doesn't harm controlling player
	
	if otherHumanoid and otherHumanoid == humanoid then
		return
	elseif otherHumanoid and isAttacking and canDamage then
		canDamage = false
		otherHumanoid:TakeDamage(Damage)
	end
end

-- On Attack Function



local function onAttack()
	
	local char = tool.Parent
	humanoid = char:WaitForChild("Humanoid")
	
	attack1 = humanoid:LoadAnimation(tool:WaitForChild("Attack1")) 
	attack2 = humanoid:LoadAnimation(tool:WaitForChild("Attack2")) 
	attack3 = humanoid:LoadAnimation(tool:WaitForChild("Attack3")) 
	attack4 = humanoid:LoadAnimation(tool:WaitForChild("Attack4")) 
	attack5 = humanoid:LoadAnimation(tool:WaitForChild("Attack5")) 
	
	local debounce = 1
	if debounce == 1 then
		-- Attack 1
		local waitTime = math.max(attack1.Length, CooldownTime)
		if not isAttacking then
			isAttacking = true -- Disable repeated attack attempts during animation
			canDamage = true
			attack1:Play() -- Play animation
			wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
			isAttacking = false -- Re-Enable attack attempts
			debounce = 2
		end
	elseif debounce == 2 then
		-- Attack 2
		local waitTime = math.max(attack2.Length, CooldownTime)
		if not isAttacking then
			isAttacking = true -- Disable repeated attack attempts during animation
			canDamage = true
			attack2:Play() -- Play animation
			wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
			isAttacking = false -- Re-Enable attack attempts
			debounce = 3
		end
	elseif debounce == 3 then
		-- Attack 3
		local waitTime = math.max(attack3.Length, CooldownTime)
		if not isAttacking then
			isAttacking = true -- Disable repeated attack attempts during animation
			canDamage = true
			attack3:Play() -- Play animation
			wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
			isAttacking = false -- Re-Enable attack attempts
			debounce = 4
		end
	elseif debounce == 4 then
		-- Attack 4
		local waitTime = math.max(attack4.Length, CooldownTime)
		if not isAttacking then
			isAttacking = true -- Disable repeated attack attempts during animation
			canDamage = true
			attack4:Play() -- Play animation
			wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
			isAttacking = false -- Re-Enable attack attempts
			debounce = 5
		end
	elseif debounce == 5 then
		-- Attack 5
		local waitTime = math.max(attack5.Length, CooldownTime)
		if not isAttacking then
			isAttacking = true -- Disable repeated attack attempts during animation
			canDamage = true
			attack5:Play() -- Play animation
			wait(waitTime) -- Wait until animation is complete or cooldown is finished whichever is longer
			isAttacking = false -- Re-Enable attack attempts
			debounce = 1
		end
	end
end

tool.Equipped:Connect(onEquip)
tool.Activated:Connect(onAttack)
swordBlade.Touched:Connect(onDetectHit)

I tried getting help in Roblox Studio Communities but no one helped me so far.
Any help is appreciated

I assume you want to chain the animations, however, only attack1 is playing over and over?
If that’s the case - you need to create variable local debounce = 1 outside of the attack function since whenever player attacks the variable gets created anew in the attack function with value 1 and thus never plays attack2-5

Also humanoid:LoadAnimation() is deprecated, you should use Animator:LoadAnimation()

1 Like

Thanks so much! This helped me learn something new and also solved my question!

1 Like