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

How do I add debounce to an animation script?

I've got a script that randomly picks one out of three animations when the player clicks, and I've tried to add debounce to it. Here's the product:

local debounce = false

local UserInputService = game:GetService("UserInputService")
    local Player = game.Players.LocalPlayer
    local Character = Player.Character
    local Humanoid = Character:WaitForChild("Humanoid")
    UserInputService.InputBegan:Connect(function(InputObject)
        if InputObject.UserInputType == Enum.UserInputType.MouseButton1 then
	debounce = true
local numberofanims = 3
local num = math.random(1,numberofanims)
	if num == 1 then
	            local Animation = Instance.new("Animation")
	            Animation.AnimationId = "rbxassetid://wasdwasdwasd"
	            local Track = Humanoid:LoadAnimation(Animation)
	            Track:Play()
	wait (1)
	elseif num == 2 then
	            local Animation = Instance.new("Animation")
	            Animation.AnimationId = "rbxassetid://wasdwasdwasd"
	            local Track = Humanoid:LoadAnimation(Animation)
	            Track:Play()
	wait(1)
	elseif num == 3 then
	            local Animation = Instance.new("Animation")
	            Animation.AnimationId = "rbxassetid://wasdwasdwasd"
	            local Track = Humanoid:LoadAnimation(Animation)
	            Track:Play()
	wait(1)
		end
     end
 end)

The problem is, the debounce has no effect. The player can still spam click their mouse and the animations are cut off. Please tell me what I did wrong.

1 answer

✓ Accepted answer

You have a debounce but the if statement isn't checking if the debounce is true or not, so the code just runs. I didn't test this, so i have no idea if it works

debounce = false

local UserInputService = game:GetService("UserInputService")
    local Player = game.Players.LocalPlayer
    local Character = Player.Character
    local Humanoid = Character:WaitForChild("Humanoid")
    UserInputService.InputBegan:Connect(function(InputObject)
        if InputObject.UserInputType == Enum.UserInputType.MouseButton1 and not debounce then
    debounce = true
local numberofanims = 3
local num = math.random(1,numberofanims)
    if num == 1 then
                local Animation = Instance.new("Animation")
                Animation.AnimationId = "rbxassetid://wasdwasdwasd"
                local Track = Humanoid:LoadAnimation(Animation)
                Track:Play()
    wait (1)
    elseif num == 2 then
                local Animation = Instance.new("Animation")
                Animation.AnimationId = "rbxassetid://wasdwasdwasd"
                local Track = Humanoid:LoadAnimation(Animation)
                Track:Play()
    wait(1)
    elseif num == 3 then
                local Animation = Instance.new("Animation")
                Animation.AnimationId = "rbxassetid://wasdwasdwasd"
                local Track = Humanoid:LoadAnimation(Animation)
                Track:Play()
           
    wait(1)
        end
wait(1)
debounce = false
     end
 end)

1 comment

0

Just what I needed, thanks!

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