Reset anim order after a certain amount of time

So lets say i swing once cooldown is over, then i wait a few seconds i want my anim to go back the first of instead of the next one, but if i spam then it will go to the next animation and so fourth.

This is the animation function in the script

local function AtkLoader()
    if Equipped then
        StopAtkAnims()
        if OrderNum == 1 then
            SwingAnim:Play(0.2)
            OrderNum = OrderNum + 1
            return
        end
        if OrderNum == 2 then
            OverHeadAnim:Play(0.2)
            OrderNum = 1
            return
        end
    end
end

What would i add or do to make it reset?

You would simply check last swing and current swing time and if it’s longer than let’s say 1 second OrderNum will go back to 1. Here is how you would do it:

local lastSwing = os.clock()

local function AtkLoader()
    if Equipped then
        StopAtkAnims()
        if os.clock() - lastSwing > 1 then -- If they don't swing in 1 second it will reset OrderNum
            OrderNum = 1
        end
        if OrderNum == 1 then
            SwingAnim:Play(0.2)
            OrderNum = OrderNum + 1
            return
        end
        if OrderNum == 2 then
            OverHeadAnim:Play(0.2)
            OrderNum = 1
            return
        end
        lastSwing = os.clock()
    end
end
1 Like

Thanks a lot, that helped. (30)

But it does only work once. (30)

Can i see your code if possible?

local lastSwing = os.clock()

local function AtkLoader()
    if Equipped then
        StopAtkAnims()
        if os.clock() - lastSwing > 7 then
            OrderNum = 1
        end
        if OrderNum == 1 then
            SwingAnim:Play(0.2)
            OrderNum = OrderNum + 1
            return
        end
        if OrderNum == 2 then
            OverHeadAnim:Play(0.2)
            OrderNum = 1
            return
        end
        lastSwing = os.clock()
    end
end
--- Activation part

Tool.Activated:Connect(function()
    if Debounce == false and Equipped then
        Debounce = true
        AtkLoader()
        Gui.Enabled = true
        Gui.Frame.CooldownFrame:TweenSize(UDim2.new(0, 69, 0, 3),"Out", "Linear", CoolDown)
        wait(CoolDown)
        Gui.Enabled = false
        Gui.Frame.CooldownFrame.Size = UDim2.new(0,0,0,3)
        Debounce = false
    end
end)

Reason why there is the 7 is because i would be giving it time to reset after the actual Debounce.

Are you sure you’re waiting 7 seconds? It works fine for me but try to print os.clock() - lastSwing inside Actiavted event to see if it’s been 7 seconds

What happens is it works after the 7 seconds but then everytime i swing after that its the first anim and prints let me get a video of it

First Video is at the top, second part of it is the second one.

Oh i see the issue, you’re returning the function after your OrderNum == value if satements so it will never update the lastSwing variables so you should remove returns or put lasSwing = os.clock() above if OrderNum == 1 then statement

1 Like

it works, I put LastSwing = os.clock() above the if statement.

1 Like

Final script

local lastSwing = os.clock()

local function AtkLoader()
    if Equipped then
        StopAtkAnims()
        if os.clock() - lastSwing > 3 then -- Didnt need to do 7 because it recognized it before as both
            OrderNum = 1
        end
        lastSwing = os.clock()
        if OrderNum == 1 then
            SwingAnim:Play(0.2)
            OrderNum = OrderNum + 1
            return
        end
        if OrderNum == 2 then
            OverHeadAnim:Play(0.2)
            OrderNum = 1
            return
        end
    end
end