Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Animation Debounce not creating a cooldown?

So, for the third time, I made a local script that plays an animation when R is pressed, along with a function for debounce, so the animation is not spammable. Here is the animation debounce function, it does not change the value for debounce, but it runs waits according to the parameters. (LocalScript)

local function WaitDebounce(AnimationTrack,DebounceLength)
    repeat wait() until AnimationTrack.IsPlaying == false

    wait(DebounceLength)
end

Now, for the local script that runs the animations:(Same local script as before)

--Humanoid is already defined bc im too lazy to write
local Attack = game.ReplicatedStorage.Attack

local AttackTrack = Humanoid:LoadAnimation(Attack)

local UIS = game:GetService("UserInputService")

local AttackDebounce = false

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.R and AttackDebounce == false then
        AttackTrack.Looped = false

        AttackTrack:Play()

        AttackDebounce = true
        WaitDebounce(AttackTrack,0.5)
        AttackDebounce = false
    end
end)

When I run the scripts, everything works completely fine except for the Debounce. Does anybody have a solution?

1 answer

✓ Accepted answer

Hello! Hope your having a great day :)

It seems you are playing the animation before the debounce, which I don't usually do, so I'd add the debounce at the very start and end of the function.

Like so:

--Humanoid is already defined bc im too lazy to write
local Attack = game.ReplicatedStorage.Attack

local AttackTrack = Humanoid:LoadAnimation(Attack)

local UIS = game:GetService("UserInputService")

local AttackDebounce = false

UIS.InputBegan:Connect(function(Input)
    if Input.KeyCode == Enum.KeyCode.R and AttackDebounce == false then
	AttackDebounce = true
        AttackTrack.Looped = false

        AttackTrack:Play()

        wait(Attack.Length + 0.5) -- replaced with whatever WaitDebounce is.
        AttackDebounce = false
    end
end)

I'm not too sure if that'll work so please comment if it doesn't :)

Thanks for reading,

  • DrShockz

5 comments

0

Ok i'll try it out in a few hours cause school lol

0

But this does seem like the only good answer

0

Thanks :)

0

I have one request, can u copy and paste your answer into roblox studio, bind the animation to the animationtrack and test if the debounce works?

0

Sorry it was late for me :)

This discussion is preserved as it was. New answers, comments, and votes are closed.