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 functioning correctly?

I made a function in a module script that plays animations, specifically attack animations. It also slows the player down when the animation is playing. And with attack animations, comes animation cool downs, or debounce. Here is the function in the module script:

local AttackAnimationModule = {}

AttackAnimationModule.PlayAttack = function(AnimationTrack)
    if AttackDebounce == true then return end --AttackDebounce is already defined in the script  that uses the module script function

    AnimationTrack.Looped = false

    AnimationTrack:Play()

    AttackDebounce = true --Turn on cooldown

    Humanoid.WalkSpeed = 3 --Slow down player
    wait(1) --The animation length
    Humanoid.WalkSpeed = 16

    AttackDebounce = false --Turn off cooldown
end

return AttackAnimationModule

And here is the script that uses the function (LocalScript):

local UIS = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer

repeat wait() until Player.Character --Load character
repeat wait() until Player.Character.Humanoid --Load humanoid

local Humanoid = Player.Character.Humanoid

local Attack = game.ReplicatedStorage.Attack
local AttackTrack = Humanoid:LoadAnimation(Attack)

local AttackAnimationModule = require(game.ReplicatedStorage.AttackAnimations)

local AttackDebounce = false

UIS.InputBegan:Connect(function(Input)
    if input.KeyCode == Enum.KeyCode.E then
        AttackAnimationModule.PlayAttack(AttackTrack)
    end
end)

When I run both scripts, the animations, and walkspeed work completely fine. But the debounce doesn't work. Any solutions?

2 comments

0

Put your debounce in the LOCAL script, not in the server script. Also, use GPE (Game Processed Event) to iron out CoreGui events.

0

I edited the answer.

1 answer

✓ Accepted answer

Put your debounce in the localscript and inside the event. Not in your server script. Your server script isn't doing anything wrong.

local attackDebounce = false

UIS.InputBegan:Connect(function(input, GPE)
	if GPE then return end --You might as well use it, in case someone types with E, which WILL happen.
	if input.KeyCode == Enum.KeyCode.E then
		if not attackDebounce then --Alternative: if deb == false
			attackDebounce = true
			AttackAnimationModule.PlayAttack(AttackTrack)
			wait(1)
			attackDebounce = false
		end
	end
end)

Note: Replace your UIS event with this one.

3 comments

0

Okay i'll try

0

How do I disable the cooldown?

0

oh sorry, I'll edit the answer

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