Debounce is too slow?

Hi, I’m using UserInputService in one of my scripts and a Debounce so that the this doesn’t happen:
image
And it still happens.
This is part of the script I used:

UserInputsService.InputBegan:Connect(function(input, GPE)
			if GPE then return end
			if DeBounce then return else DeBounce = true end

			print("Pressed "..tostring(input.KeyCode).."!")

			-- Does something here

			DeBounce = false
		end)

Is the script to slow, and by the time it’s at the 3rd line, another event fires?

Solution has been found, however, I still don’t understand why it says I pressed a key 3 times if I only pressed it once.

Try using this?

UserInputsService.InputBegan:Connect(function(input, GPE)
	if not DeBounce and not GPE then

DeBounce=true

		print("Pressed "..tostring(input.KeyCode).."!")

			-- Does something here

wait(1) -- change amount if you'd like

			DeBounce = false
		end)

If there is any problems blame my tab key /j

It would work, but I’m using this in a race, so I have to handle this as fast as possible.

Well, you’re clicking the button 3 times and it’s registering 3 times. The only way a debounce actually works is by blocking other signals while it’s processing the ongoing one.

you can set the

task.wait()

to something like 1/10th of a second

Lua is single-threaded so this is not possible.

You could use a timestamp to ensure inputs are at least a set time apart:

local debounce = false
local timestamp = 0
local min_interval = 0.01
UserInputService.InputBegan:Connect(function(input, gpe)
	if gpe or debounce then return end
	if os.clock() - timestamp < min_interval then return end
	timestamp = os.clock()
	debounce = true
	
	-- code
	
	debounce = false
end)

I would suspect what is happening is that your entire function is too fast, so the debounce is completed too quickly and the subsequent duplicate ‘fires’ are processed. You need a small pause (wait) before debounce = false
maybe .1 or .01 seconds depending on how quickly you want to rearm the button.

What are you even trying to do here? There is no meaning to a debounce if there is no cooldown between actions.

P.S. XY Problems and You: Develop better questions for your problems

2 Likes

It works now, but why does it think I pressed a key 3 time?

because you did not put something that detects a specific key

local debounce = false
local timestamp = 0
local min_interval = 0.01
local keycode = "E"
UserInputService.InputBegan:Connect(function(input, gpe)
	if gpe or debounce then return end
if input.KeyCode~=Enum.KeyCode[keycode] then return end
	if os.clock() - timestamp < min_interval then return end
	timestamp = os.clock()
	debounce = true
	
	-- code

task.wait(min_interval)
	
	debounce = false
end)

That part was in the Does something here part of the script:

UserInputsService.InputBegan:Connect(function(input, GPE)
			if GPE then return end
			if DeBounce then return else DeBounce = true end

			print("Pressed "..tostring(input.KeyCode).."!")
			if input.KeyCode == Enum.KeyCode[currentKey.Value] then
				player.Character:WaitForChild("HumanoidRootPart").CFrame = player.Character:WaitForChild("HumanoidRootPart").CFrame + Vector3.new(0,0,Speed)
				randomize()
			elseif table.find(inputs,input.KeyCode) then
				incorrectSound:Play()
				player.Character:WaitForChild("HumanoidRootPart").CFrame = player.Character:WaitForChild("HumanoidRootPart").CFrame - Vector3.new(0,0,Speed)
			end
			
			wait()
			
			DeBounce = false

Maybe because the print line is before the one that detects a specific key?

that could be why, if you put it after the next line it should print only if the player presses the correct key

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.