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

(SOLVED) How do I add a debounce?

Asked by
VVTF_RU 18
4 years ago
Edited 4 years ago

Please encode Lua code in the Lua block code tag (look for the Lua icon in the editor).

I am very very very new to coding and I have 2 scripts for a tool. One of said scripts is for adding speed, and the other is for an animation. How do I add a debounce so you cant spam autoclick?

Speed script

script.Parent.Equipped:Connect(function()
 script.Parent.Activated:Connect(function()
  local Hum = script.Parent.Parent:FindFirstChild("Humanoid")
          Hum.WalkSpeed = Hum.WalkSpeed + 0.10
 end)
end)

Animation Script

script.Parent.Equipped:Connect(function(Mouse)
    Mouse.Button1Down:Connect(function()
        animation = game.Players.LocalPlayer.Character.Humanoid:LoadAnimation(script.Parent.Animation)
        animation:Play()
    end)
end)

script.Parent.Unequipped:Connect(function()
            animation:Stop()
end)
0
Fixed my answer User#30567 0 — 4y

2 answers

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago
-- 8/11/2020 0:24AM
-- AswormeDorijan111

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

repeat wait() until Player.Character

local Char = Player.Character;

local Humanoid = Char:WaitForChild("Humanoid");

local Debounce = true;

local IsEquipped = false;

local Cooldown = 2; -- In seconds

local SpeedIncrease = 0.1;

local IncreaseSpeedAfterAnim = true; -- Change this to false if you don't want to give the player speed once the animation is done 

local Animation = Humanoid:LoadAnimation(script.Parent:WaitForChild("Animation"));

script.Parent.Equipped:Connect(function()
    IsEquipped = true;
end)

script.Parent.Activated:Connect(function()
    if not debounce then
        return
    end
    Debounce = false;

    Animation:Play()

    if IncreaseSpeedAfterAnim then
        Animation.Stopped:Wait()
        Humanoid.WalkSpeed = Humanoid.WalkSpeed + SpeedIncrease;
    else
        Humanoid.WalkSpeed = Humanoid.WalkSpeed + SpeedIncrease;
    end

    wait(Cooldown);
    Debounce = true;
end)

script.Parent.Unequipped:Connect(function()
    IsEquipped = false;
    Animation:Stop()
end)


Put everything in a single Local Script and you're good to go.

0
Thank you so much, it works JUST as I wanted and has no flaws. VVTF_RU 18 — 4y
0
Glad to hear that I've helped. AswormeDorijan111 531 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Please provide explanation with your answers. Simply posting code does not spread knowledge of integral scripting processes which helps people understand the logic and reasoning behind your answer.

Use this script:

local SpeedUpTool = script.Parent
local Humanoid, Character
local Debounce = false
if SpeedUpTool.Parent:IsA("Model") then
    Character = SpeedUpTool.Parent
    Humanoid = Character.Humanoid
elseif SpeedUpTool.Parent.Parent:IsA("Player") then
    Character = SpeedUpTool.Parent.Parent.Character or SpeedUpTool.Parent.Parent.CharacterAdded:Wait()
    Humanoid = Character.Humanoid
end
local Animation = Humanoid:LoadAnimation(script.Parent.Animation)
SpeedUpTool.Activated:Conect(function()
    if not Debounce then    
        Debounce = true
        Humanoid.WalkSpeed += 0.1
        Animation:Play()    
        wait(Animation.Length)
        Debounce = false
    end
end)
0
I attempted to use your script and it said Script 'Players.ButterDoesFly.Backpack.Speed.LocalScript', Line 7 so I'm assuming I change it to match my tool? VVTF_RU 18 — 4y
0
Where did it say that? please don't change the script User#30567 0 — 4y
0
Can you show us the exact text in your question? User#30567 0 — 4y
0
ok sorry User#30567 0 — 4y
View all comments (6 more)
0
This only errors in studio User#30567 0 — 4y
0
I tried playing in the game normally and it did not work. VVTF_RU 18 — 4y
0
17:44:06.473 - Stack Begin 17:44:06.474 - Script 'Players.ButterDoesFly.Backpack.Speed.Script', Line 11 17:44:06.475 - Stack End VVTF_RU 18 — 4y
0
DId you see the link? User#30567 0 — 4y
0
Yeah, it just confused me more cause I have no idea what they are talking about... VVTF_RU 18 — 4y

Answer this question