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.
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
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