Adding a timer to a combo before it resets for my sword tool?

  1. What do you want to achieve? I would like for the combo to reset after 2 seconds if they don’t click a second time within that time frame.

  2. What is the issue? I can’t seem to find the right way to add a “Timer.”

  3. What solutions have you tried so far? I’ve looked through various tutorial videos, but none seem to be as simple as I’m anticipating this being with my current work. I’ve tried to utilize “tick()” but I don’t think I’m doing it right, as it doesn’t seem to cause an error when I add it, but nor does it work how I want it to.

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local human = character:WaitForChild("Humanoid")
local idleAnimation = human:LoadAnimation(game.ReplicatedStorage.Animations.Sword:WaitForChild("idle1"))
local attackAnimation1 = human:LoadAnimation(game.ReplicatedStorage.Animations.Sword:WaitForChild("swing1"))
local attackAnimation2 = human:LoadAnimation(game.ReplicatedStorage.Animations.Sword:WaitForChild("swing2"))
local streak = 0
local D = true

tool.Equipped:Connect(function()
	idleAnimation:Play()
end)

tool.Activated:Connect(function()
	if streak == 0 and D == true then
		D = false
		attackAnimation1:Play()
		streak = streak + 1
		task.wait(1)
		D = true
	end
end)

tool.Activated:Connect(function()
	if streak == 1 and D == true then
		D = false
		attackAnimation2:Play()
		streak = streak - 1
		task.wait(1)
		D = true
	end
end)


tool.Unequipped:Connect(function()
	idleAnimation:Stop()
end)

Any insight or at least a thought to get me in the right direction would be helpful. Eventually I would like to add damage & stun animations to enemies, but just animating is the first step in this journey.

You’ll want to use a debounce with a timer. This will reset the streak if there isn’t a second click within 2 seconds. Play with the values to meet your needs!

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local human = character:WaitForChild("Humanoid")
local idleAnimation = human:LoadAnimation(game.ReplicatedStorage.Animations.Sword:WaitForChild("idle1"))
local attackAnimation1 = human:LoadAnimation(game.ReplicatedStorage.Animations.Sword:WaitForChild("swing1"))
local attackAnimation2 = human:LoadAnimation(game.ReplicatedStorage.Animations.Sword:WaitForChild("swing2"))
local streak = 0
local D = true

local function resetStreak()
    task.wait(2)  -- Wait for 2 seconds
    if streak == 1 then  -- Check if the streak is still 1
        streak = 0
        print("Streak reset due to inactivity")
    end
end

tool.Equipped:Connect(function()
    idleAnimation:Play()
end)

tool.Activated:Connect(function()
    if streak == 0 and D == true then
        D = false
        attackAnimation1:Play()
        streak = streak + 1
        task.delay(2, resetStreak)  -- Start the timer to reset the streak
        task.wait(1)
        D = true
    elseif streak == 1 and D == true then
        D = false
        attackAnimation2:Play()
        streak = 0  -- Reset the streak after the second attack
        task.wait(1)
        D = true
    end
end)

tool.Unequipped:Connect(function()
    idleAnimation:Stop()
end)
1 Like

Thank you for this, I noticed you used ElseIf instead of what I had. Regardless, this was exactly the fix I needed. I’d assume if I wanted to add a third combo attack I would just put…

local function resetStreak()
    task.wait(2)  -- Wait for 2 seconds
    if streak >= 1 then  -- Check if the streak is greater than 1
        streak = 0
        print("Streak reset due to inactivity")
    end
end

And then add another ElseIf statement within?

1 Like

Correct! You can modify the resetStreak function as you suggested. Then, you would add another elseif statement within the tool.Activated function to handle the third combo attack.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.