How do I check if a key has been pressed two times in a certain period of time?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to check if the key W has been pressed 2 times within a certain period of time.
  2. What is the issue? Include screenshots / videos if possible!
    I don’t know how to do it.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I don’t know how to attempt this.

I’m not asking for a script. However, what is a simple and an efficient way to do it?
Thank you

2 Likes

I know you asked for a way to do it and not necessarily the script itself, but I have no idea how I would describe the script so I just created it below. It should work.

local UIS = game:GetService("UserInputService")
local LastPress = 0
local CertainPeriodOfTime = 60 -- seconds
UIS.InputBegan:Connect(function(key)
 if key.KeyCode = Enum.KeyCode.W then
  if os.time() - LastPress < CertainPeriodOfTime then
   --Then the user has pressed W twice in the period of time
  end
  LastPress = os.time()
 end
end)

4 Likes

Inside the user input function, check if a variable is true or false. If it is false, set it to true, wait X seconds, then set it to false. If it is true, meaning the player has clicked it twice in X seconds, execute the code you want to.

I’d advise using tick() rather than os.time() for this, as it allows decimals which is much more accurate and consistent for smaller times.

This is a pretty simple solution, simply get the time that the first time they press w, then subtract is by the second time:

local UIS = game:GetService("UserInputService")

local PreviousInput = 0 -- Sets the variable even before anything is pressed to shorten code
local TimeThreshold = 1 -- The time to detect 2 "w" keys pressed down

UIS.InputBegan:Connect(function(Key)
    if Key.KeyCode == Enum.KeyCode.W then
        if tick() - PreviousInput <= TimeThreshold then -- Checks if the current time subtracted by the previous time is smaller or equal to the TimeThreshold
            -- do something
        end
    else
        PreviousInput = tick() -- Makes it the current time
    end
end)
3 Likes

PreviousInput should have been set to 0 at the start of the game, otherwise the user could simply press W once when the game starts and have tick() - PreviousInput be less than TimeThreshold.

2 Likes

I know this is a little late, but wouldn’t this work also?? Assuming the wait() is the amount of time you wanna wait between clicks if it should work. For this I just made it so it’s double tap to sprint.

local char = script.Parent
local hum = char:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
local canSprint = false

uis.InputBegan:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.W and canSprint == false then
		canSprint = true
		wait(0.4)
		canSprint = false
	elseif key.KeyCode == Enum.KeyCode.W and canSprint == true then
		hum.WalkSpeed = 50
	end
end)

uis.InputEnded:Connect(function(key)
	if key.KeyCode == Enum.KeyCode.W and canSprint == false then
		hum.WalkSpeed = 16
	elseif key.KeyCode == Enum.KeyCode.W and canSprint == true then
		hum.WalkSpeed = 50
	end
end)