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

How do I add debounce to an animation script?

Asked by
Lyphios 77
4 years ago
Edited 4 years ago

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

Log in to vote
0
Answered by
bum5Br 97
4 years ago

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)
0
Just what I needed, thanks! Lyphios 77 — 4y
Ad

Answer this question