Debounce script help

I know there are several ways to acheive a debounce in a script. This is the way I normally do it, but it doesn’t work in this script. I am assuming because there are two if statements near each other. Anyone able to show me another way to debounce this script.

Thanks

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key)
	if Debounce then return end
	Debounce = true
	if key.KeyCode == Enum.KeyCode.Q then
		Animation:Play()
		Sound:Play()
		

	end		
	
	
	if key.KeyCode == Enum.KeyCode.E then
		Animation2:Play()
		Sound2:Play()
					
			
	end
	
	wait(5)
	Debounce = false
	
end)
local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key)
	if not Debounce then
        Debounce = true
	    if key.KeyCode == Enum.KeyCode.Q then
		   Animation:Play()
		   Sound:Play()
        end

        if key.KeyCode == Enum.KeyCode.E then
		   Animation2:Play()
		   Sound2:Play()		
        end

        task.wait(5)
        Debounce = falase
	end		
end)

The post above should be solving your problem but
debounce is being turned to true on input thus if you try walking or press any single key on the keyboard the debounce will be set to true.
I suggest you set debounce to true only when that certain key is pressed.

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key)
	if not Debounce then return end
	
	if key.KeyCode == Enum.KeyCode.Q then Debounce = true 
		Animation:Play()
		Sound:Play()
		

	end		
	
	
	if key.KeyCode == Enum.KeyCode.E then Debounce = true
		Animation2:Play()
		Sound2:Play()
					
			
	end
	
	wait(5)
	Debounce = false
	
end)

I tried this and the animations wont work.

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Animation3 = Humanoid:LoadAnimation(script:WaitForChild("crouch"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key)
	if not Debounce then
		Debounce = true

		if key.KeyCode == Enum.KeyCode.Q then
			Animation:Play()
			Sound:Play()
		
		end		
		
		if key.KeyCode == Enum.KeyCode.E then
			Animation2:Play()
			Sound2:Play()
								
		end
		
		task.wait(5)
		Debounce = false
		
	end		
end)

but removing the debounce works makes the animations work again.

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Animation3 = Humanoid:LoadAnimation(script:WaitForChild("crouch"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key)

	if key.KeyCode == Enum.KeyCode.Q then
		Animation:Play()
		Sound:Play()
		
	end		
		
	if key.KeyCode == Enum.KeyCode.E then
		Animation2:Play()
		Sound2:Play()
			
	end
end)

I tried this also, and the animations wont play.

you need to replace the 2nd if statement with an elseif while inside the first if statement’s end thingy

like this btw

if key.KeyCode == Enum.KeyCode.Q then
		Animation:Play()
		Sound:Play()
elseif key.KeyCode == Enum.KeyCode.E then
		Animation2:Play()
		Sound2:Play()
	end

i almost forgot that due to not getting the second default return of your input event “the game processed event” the input may fire while typing so you have to do some adjustments

UIS.InputBegan:Connect(function(key, gp)
	if not Debounce and not gp then

Thanks for your help. It is giving my a red line under end). Does this look right?

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Animation3 = Humanoid:LoadAnimation(script:WaitForChild("crouch"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key, gp)
	if not Debounce and not gp then

	if key.KeyCode == Enum.KeyCode.Q then
		Animation:Play()
		Sound:Play()
		
	elseif key.KeyCode == Enum.KeyCode.E then
		Animation2:Play()
		Sound2:Play()
	end
	task.wait(5)
	Debounce = false			
		
end)

This is much more complicated than say, debouncing a door script.

UIS.InputBegan:Connect(function(key, gp)
	if not Debounce and not gp then

	if key.KeyCode == Enum.KeyCode.Q then
		Animation:Play()
		Sound:Play()
		
	elseif key.KeyCode == Enum.KeyCode.E then
		Animation2:Play()
		Sound2:Play()
	end
	task.wait(5)
	Debounce = false
    end
end)

you forgot to add an extra end

This plays the animations but does not debounce.

Should I add a Debounce = true under the if not Debounce and not gp then?

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key, gp)
	if not Debounce and not gp then

	if key.KeyCode == Enum.KeyCode.Q then
		Animation:Play()
		Sound:Play()
		
	elseif key.KeyCode == Enum.KeyCode.E then
		Animation2:Play()
		Sound2:Play()
	end
	task.wait(5)
	Debounce = false			
	end		
end)

yeah you can sussily do that (30)

I tried this. Animations wont play. I am totally confused.

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key, gp)
	if not Debounce and not gp then
		Debounce = true

		if key.KeyCode == Enum.KeyCode.Q then
			Animation:Play()
			Sound:Play()

		elseif key.KeyCode == Enum.KeyCode.E then
			Animation2:Play()
			Sound2:Play()
		end
		task.wait(5)
		Debounce = false			
	end		
end)

btw, are the sounds playing? if your animation is the only one that isn’t working tell me if you don’t own it at all and took it from somewhere else, to fix this problem you need to save the animation exports and replace the animation id with yours like an opposite NFT

if I add that Debounce = true line, nothing plays. No sound, no animations.

This script plays sound and animations, but I can spam the key as fast as I want and the debounce does nothing.

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key, gp)
	if not Debounce and not gp then

		if key.KeyCode == Enum.KeyCode.Q then
			Animation:Play()
			Sound:Play()

		elseif key.KeyCode == Enum.KeyCode.E then
			Animation2:Play()
			Sound2:Play()
		end
		task.wait(5)
		Debounce = false			
	end		
end)

ok, try putting the debounce inside the if key statements and see if it works, i don’t know anything else why this is being caused

1 Like

Bingo, you got it. Debounce inside the if statement is the key.

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key, gp)
	if not Debounce and not gp then

		if key.KeyCode == Enum.KeyCode.Q then
			Debounce = true
			Animation:Play()
			Sound:Play()

		elseif key.KeyCode == Enum.KeyCode.E then
			Debounce = true
			Animation2:Play()
			Sound2:Play()
		end
		task.wait(2)
		Debounce = false			
	end		
end)

Actually this works even better.

local UIS = game:GetService("UserInputService")
local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
local Animation = Humanoid:LoadAnimation(script:WaitForChild("taunt"))
local Animation2 = Humanoid:LoadAnimation(script:WaitForChild("laugh"))
local Debounce = false
local Sound = script.Parent.Head.Yell
local Sound2 = script.Parent.Head.Laugh

UIS.InputBegan:Connect(function(key, gp)
	if not Debounce and not gp then

		if key.KeyCode == Enum.KeyCode.Q then
			Debounce = true
			Animation:Play()
			Sound:Play()
			
			task.wait(3)
			Debounce = false
			
		elseif key.KeyCode == Enum.KeyCode.E then
			Debounce = true
			Animation2:Play()
			Sound2:Play()
			
			task.wait(3)
			Debounce = false	
		end		
	end		
end)

Thanks again for helping me with this.

1 Like