How would I use debounce to wait for my attack animation to stop before i can play another attack animation?

Im working on my combat system and i ran into a problem. Spamming attack animations. How can i fix this with debounce? Im testing my combat system and whenever i spam attack it just cuts off the current animation and goes to the next. How can i wait for my attack animations to finish before i can activate the next?

wait(3)

local Player = game.Players.LocalPlayer
local Humanoid = Player.Character.Humanoid -- Humanoid

Player = game.Players.LocalPlayer.Character
Animation1 = Player.Humanoid:LoadAnimation(script.Slices)
Animation2 = Player.Humanoid:LoadAnimation(script.Spin)
Animation3 = Player.Humanoid:LoadAnimation(script.BackFlipStab)
Animation4 = Player.Humanoid:LoadAnimation(script.DownSlice)
CanFly = true

script.Parent.Parent.Activated:connect(function()
	local Fly = math.random(1,4)
	if Fly == 1 and CanFly == true then
		Humanoid.WalkSpeed = 0
		Humanoid.JumpPower = 0
		Animation1:Play()
		wait(1) -- Amount of time HumanoidRootPart is anchored after animation
		CanFly = false
		Humanoid.WalkSpeed = 16
		Humanoid.JumpPower = 50
		CanFly = true
		
	else if Fly == 2 and CanFly == true then
			Humanoid.WalkSpeed = 0
			Humanoid.JumpPower = 0
		Animation2:Play()
		CanFly = false
			wait(1) -- Amount of time HumanoidRootPart is anchored after animation
			Humanoid.WalkSpeed = 16
			Humanoid.JumpPower = 50
		CanFly = true
		
		else if Fly == 3 and CanFly == true then
				Humanoid.WalkSpeed = 0
				Humanoid.JumpPower = 0
		Animation3:Play()
		CanFly = false
				wait(1) -- Amount of time HumanoidRootPart is anchored after animation
				Humanoid.WalkSpeed = 16
				Humanoid.JumpPower = 50
		CanFly = true
		
			else if Fly == 4 and CanFly == true then
					Humanoid.WalkSpeed = 0
					Humanoid.JumpPower = 0
		Animation4:Play()
		CanFly = false
					wait(1) -- Amount of time HumanoidRootPart is anchored after animation
					Humanoid.WalkSpeed = 16
					Humanoid.JumpPower = 50
		CanFly = true
		
		   end
		end
		end
	    end
	    end)

1 Like

btw i just realized how bad ā€œanchored AFTER animationā€ looks in my script notes. Itā€™s actually anchored during the animations so players cant just run around swinging there weapon.

Debounces work like this:

local debounce = false

script.Parent.Parent.Activated:Connect(function()
	if debounce == false then
		debounce = true
		--//Main script here
		task.wait(1)
		debounce = false
	end
end)
2 Likes

You would use a variable of any name (usually ā€˜debounceā€™), it would be a bool which you set to true at the beginning (but after your condition), and then you add a condition that makes it so that none of your attack code runs when debounce is true, you would then set debounce to false at the end of your attack code.

1 Like
--see if this works:
Animation.Stopped:wait() -- i used this before, probaly will work
1 Like

For some weird reason im not really a fan of Animation.Stopped lol but it does work, but for some reason it just behaves oddly for me.

Thank you! This worked perfectly.

For example:

local streak = 0

local Ataque = 0

local D = false
this is what i made, may not too good but this works very nice

Mouse.Button1Down:Connect(function() -- Example
Ataque = tick() 
if tick() - Ataque <= 1.5 then -- 
if streak == 0 and D == false then 
		D = true
	plr.Character.Humanoid.WalkSpeed = 0
		Atk:Play()
		
		Atk.Stopped:Wait(0.8)
		
		plr.Character.Humanoid.WalkSpeed = 16
					
		streak = streak + 1 
		print"Attack 0"
	D = false
			task.wait(0.5)
	if tick() - Attack >= 0.5 then
	streak = 0 -- Reset my combo
	print"Tick 0"
	end

	else if streak == 1 and D == false then
		Atk1Elapsed:Stop()
		D = true
	        plr.Character.Humanoid.WalkSpeed = 0
		Atk2:Play()

		Atk2.Stopped:wait(0.8)
	        plr.Character.Humanoid.WalkSpeed = 16
					
		streak = streak + 1 
		print"Attack 1"
	D = false
			task.wait(0.5)

	if tick() - Ataque >= 0.5 then

                streak = 0
		print"Tick 1"
	end

	else if streak == 2 and D == false then
			D = true
			Atk3:Play()
		plr.Character.Humanoid.WalkSpeed = 0

						
		Atk3.Stopped:Wait(0.8)
		plr.Character.Humanoid.WalkSpeed = 16

		streak = streak + 1
		print"Atk 2"
D = false
		task.wait(0.5)	
	if tick() - Ataque >= 0.5 then							
		streak = 0 -- Reseting 
	        print"Elapsed too much time "
	end
								
		else if streak == 3 and D == false then
		D = true
		Atk4:Play()
		
		plr.Character.Humanoid.WalkSpeed = 0

		Atk4.Stopped:Wait(0.8)

		plr.Character.Humanoid.WalkSpeed = 16
									

		streak = 0 -- Reset because this was the third attack while in Combo
		print"Atk 3 + Reset"
		D = false
		task.wait(0.5)	
	if tick() - Ataque >= 0.5 then
		streak = 0
		print"Tick 3"
	end									
end
end
end
end  
end
end)

Edit: the function above works like this:

If you Click, and wait too much before clicking again, [The combo will reset]
This will happen even if you are in the middle of 2~3 combo streak

1 Like

Ah. i like the combo reset function. That could come in handy on one of my weapons. I will give this a try and see how it goes!