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

debounce only works once, then player can spam key. what's the issue?

the player presses and holds F, animation and other things go on in the background. then there is a cooldown. after this cooldown the debounce doesn't do it's job any more. how do i fix this?

local UserInputService = game:GetService("UserInputService")
local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()

local KeyPressed = false

repeat wait() until Player.Character
c = Player.Character
humanoid = c.Humanoid

local Time = 0
local Debounce = true

--animation codes

	local holdAnim = Instance.new("Animation")
	holdAnim.AnimationId = "http://roblox.com/asset/?id=4896264874"
	local holdTrack = humanoid:LoadAnimation(holdAnim)

	
	local chargeAnim = Instance.new("Animation")
	chargeAnim.AnimationId = "http://roblox.com/asset/?id=4896212792"
	local chargeTrack = humanoid:LoadAnimation(chargeAnim)

	local releaseAnim = Instance.new("Animation")
	releaseAnim.AnimationId = "http://roblox.com/asset/?id=4896256143"
	local releaseTrack = humanoid:LoadAnimation(releaseAnim)

--function

UserInputService.InputBegan:Connect(function(input, gameProcessed)
	if Debounce == false then return end
	if input.KeyCode == Enum.KeyCode.F then
		KeyPressed = true
		Debounce = false
		
		local BG = Instance.new("BodyGyro", c.HumanoidRootPart)
		BG.maxTorque = Vector3.new(250000,250000,250000)
		BG.CFrame = CFrame.new(c.HumanoidRootPart.Position, mouse.hit.p)
		BG.P = 20000
		local BP = Instance.new("BodyPosition", c.HumanoidRootPart)
		BP.maxForce = Vector3.new(math.huge, math.huge, math.huge)
		BP.Position = c.HumanoidRootPart.Position
		
		chargeTrack:Play()

spawn(function()
		while KeyPressed == true and Time < 200 do
			wait()
			BG.CFrame = CFrame.new(c.HumanoidRootPart.Position, mouse.hit.p)	
			Time = Time + 1
			print("Key being held interval: ")
			holdTrack:Play()
			holdTrack.Looped = true
		end
		if KeyPressed == false or Time >= 200 then
		Time = 0
		holdTrack.Looped = false		
		releaseTrack:Play()
		wait(0.5)
		BG:Remove()
		BP:Remove()
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		KeyPressed = false
		print("Key has been let go of")
			end
			wait(5)
			Debounce = true
		end)
	end
end)

2 answers

✓ Accepted answer

You set the debounce to true outside of your conditional loop, meaning any input would trigger the debounce being set back to true. Simply move it inside of your loop.

UserInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then
        KeyPressed = false
        print("Key has been let go of") 
	     wait(5)
            Debounce = true
            end
            end
        end)
    end
end)
UserInputService.InputEnded:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.F then
        KeyPressed = false
        print("Key has been let go of") 
         wait(5)
            Debounce = false
            end
            end
        end)
    end
end)

try setting debounce to false

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