I'm working on scripting a sword, and I plan on making different animations for attacking. Currently, if you click, you will get a single slash animation. If you double click, you will get that slash, along with another slash animation. If you triple click, you will get both slash animations in order, along with a third. The problem is that the script lags heavily the more that you click. I set up debounces to try and solve the issue but it doesn't seem to work. Any ideas?

local plr = game.Players.LocalPlayer
local mouse = plr:GetMouse()

local char = workspace:WaitForChild(plr.Name)
local humanoid = char:WaitForChild("Humanoid")

local Swing1 = humanoid:LoadAnimation(script.Swing1)
local Swing2 = humanoid:LoadAnimation(script.Swing2)
local Swing3 = humanoid:LoadAnimation(script.Swing3)

local BladeHitBox = script.Parent.BladeHitBox
local BladeActive = BladeHitBox.Active
local Atk = BladeHitBox.AttackDmg
local Stam = BladeHitBox.StaminaDmg

local debounce1 = false
local debounce2 = false
local debounce3 = false

script.Parent.Activated:Connect(function()
	if debounce1 == true then return end
	print("You Swung")
	BladeActive.Value = true
	Swing1:Play()
	BladeActive.Value = false
	
	script.Parent.Activated:Connect(function()
		if debounce2 == true then return end
		print("You Double Swung")
		BladeActive.Value = true
		Swing2:Play()
		BladeActive.Value = false
			
		script.Parent.Activated:Connect(function()
			if debounce3 == true then return end
			print("You Triple Swung")
			BladeActive.Value = true
			Swing3:Play()
			BladeActive.Value = false
			debounce3 = false
				
		end)
		debounce2 = false
	end)
	debounce1 = false
	print("Animation Done")
end)

Output:

You Swung

Animation Done

You Double Swung

You Swung

Animation Done

You Double Swung

You Triple Swung

You Double Swung

You Swung

Animation Done

You Double Swung

You Triple Swung (x2)

You Double Swung

You Triple Swung

You Double Swung

You Swung

Animation Done

You Double Swung

You Triple Swung (x3)

You Double Swung

You Triple Swung (x2)

You Double Swung

You Triple Swung

You Double Swung

You Swung

Animation Done

5 comments

0

Why are you nesting events inside events

0

How else could I make it so that an event is only active after another event was fired?

0

That won't work. You'll need a counter

0

What is a counter?

0

Make a variable. local counter = 0 and when the tool is activated, counter = counter + 1. Then have an if statement to check if counter is equal to 3