Hi, I’m using UserInputService in one of my scripts and a Debounce so that the this doesn’t happen:
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.
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)
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 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.
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?