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

(SOLVED) How do I add a debounce?

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)

1 comment

0

Fixed my answer

2 answers

✓ Accepted answer
-- 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.

2 comments

0

Thank you so much, it works JUST as I wanted and has no flaws.

0

Glad to hear that I've helped.

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)

10 comments

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?

0

Where did it say that? please don't change the script

0

Can you show us the exact text in your question?

0

ok sorry

0

This only errors in studio

0

I tried playing in the game normally and it did not work.

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

0

DId you see the link?

0

Yeah, it just confused me more cause I have no idea what they are talking about...

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