Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Cool-down for swords special attack animation not working properly?

Asked by 6 years ago
Edited 6 years ago
function onKeyPress(actionName, userInputState, InputObject) --KeyPress function
    local attackanim = hum:LoadAnimation(Tool.specialattack1) --Load animation
    if userInputState == Enum.UserInputState.Begin and cooldown == 0 and swordequipped == 1 then --Conditions to be met
        attackanim:Play() --Play animation
        cooldown = cooldown + 1 --Activate cooldown
        print("Cooldown Added")
    elseif cooldown == 1 then
        wait(5)
        cooldown = 0 --Remove cooldown
        print("Cooldown Removed")
    end
end
game.ContextActionService:BindAction("KeyPress", onKeyPress, false, Enum.KeyCode.R) --If R key pressed do onKeyPress function

So basically I'm trying to make it so that the special attack animation can only be used every 5 seconds, the cool-down works once but then any subsequent presses allows me to spam the animation indefinitely. Help would be appreciated.

0
Have you tried making “cooldown” equal to one instead of adding to one? It could somehow go over 1 and break the cooldown. User#20279 0 — 6y

2 answers

Log in to vote
0
Answered by
hellmatic 1523 Moderation Voter
6 years ago
function onKeyPress(actionName, userInputState, InputObject) --KeyPress function
    local attackanim = hum:LoadAnimation(Tool.specialattack1) --Load animation
    if userInputState == Enum.UserInputState.Begin and cooldown == 0 and swordequipped == 1 then --Conditions to be met
        attackanim:Play() --Play animation

    repeat wait() until not attackanim.IsPlaying -- wait till the animation is finished playing

        cooldown = cooldown + 1 --Activate cooldown
        print("Cooldown Added")

    elseif cooldown == 1 then
        --wait(5) if this is to wait for the previous animation to stop playing, you wont need it
        -- add a repeat wait() here if needed (make sure its always before setting the cooldown to 0)
     cooldown = 0 --Remove cooldown
        print("Cooldown Removed")
    end
end
game.ContextActionService:BindAction("KeyPress", onKeyPress, false, Enum.KeyCode.R) --If R key pressed do onKeyPress function
0
if you don't like repeat wait(), you can use: wait(attackanim.Length). See more here: https://wiki.roblox.com/index.php?title=API:Class/AnimationTrack hellmatic 1523 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Here this should work.

Attack = false

function sAttack() --KeyPress function
    local attackanim = hum:LoadAnimation(Tool.specialattack1) --Load animation
    attackanim:Play() --Play animation
end

function onKeyPress(actionName, userInputState, InputObject)
    if userInputState == Enum.UserInputState.Begin and swordequipped == 1 and Attack == false then --Conditions to be met
        Attack = true
        wait(5)
        sAttack()
        Attack = false
    end
end

game.ContextActionService:BindAction("KeyPress", onKeyPress, false, Enum.KeyCode.R) --If R key pressed do onKeyPress function
0
I think the `wait()` should be above the `sAttack()` SebbyTheGODKid 198 — 6y

Answer this question