How can i check if player tapped a key two times?

Hello, i have a question, how can i check if player tapped a key two times?

For example, i want to check if player tapped ‘W’ two times in 0.8 second. And can you say which line i can do after ‘W’ tapped two times?

I searched this a long, but i couldnt find what i want or i didnt understand.

1 Like

Here is a forum link I fell on while I had the same question as you. It’s not UserInputService but still you can implement it.

local last = 0

game:GetService("UserInputService").InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.W then
        local now = tick()
     
        if not last or (now - tick()) > 2 then
            last = tick()
        elseif (now - last) <= 2 then
             last = nil
             print("do stuff")
        end
    end
end)

It’s using tick() as a debounce.

3 Likes

I tried to make something. But nothing happened. I tried to print something. It said: attempt to perform arithmetic (sub) on number and function
EDIT: I put script in starterCharacter.Should I change it?
EDIT2:(i cant post anymore so i did this.) Still Same

Change local last to local last = 0
Edit: Yes put it in StarterCharacterScript as a LocalScript.

1 Like

The error happens because of this:

(now - tick) > 2

You can’t subtract a function from a number, so you’ll have to change it to:

(now - last) > 2

I didn’t realise this mistake in my post, until now

2 Likes

Ohhh right! I forgot to mention I edited that in my script when I used it. Let me go change it real quick.
Edit: Go check my first post in this topic.

2 Likes

It worked but i tried to put some variables but variables didnt exist.
where i put variable:
local last
game:GetService(“UserInputService”).InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
local now = tick()

    if not last or (now - last) > 2 then
        last = tick()
    elseif (now - last) <= 2 then
         last = nil

–Here to

	-- Here
    end
end

end)

I don’t understand what you’re trying to do. What exactly are you trying to do? Could you elaborate?

Something doesnt exist

You technically have defined the variable something, just that you haven’t set it to anything so it defaults to nil. You aren’t doing anything with it though, and the red line is there over the word something beneath where the variable is defined because you need to write an expression. If you wrote something like something = true there, the red line wouldn’t be there. What are you using this for anyways?

i tried to make local BV = Instance.new(“BodyVelocity”)

but BV didnt exist.

EDIT: Im making a dash system. I know how to make dash but idk detect if player pressed a key two times.

You have been given a script to detect if there’s two key presses within a certain time period

What do you mean by this? Are you not setting its parent when you create it?

You can try something like this:

local last = 0

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")

game:GetService("UserInputService").InputBegan:Connect(function(input)
    if input.KeyCode == Enum.KeyCode.W then
        local now = tick()
     
        if not last or (now - last) > 2 then
            last = tick()
        elseif (now - last) <= 2 then
             last = nil
             local bv = Instance.new("BodyVelocity")
             bv.Velocity = hrp.CFrame.lookVector * 50
             bv.Parent = hrp
             wait(0.2)
             bv:Destroy()
        end
    end
end)

no, when make a variable like local something = true and try to use it , it says: Myproblem2

I assume you’re doing something along the lines of this:

local something
something -- you just put something here

As stated in the script analysis, the compiler expects an assignment or a function call, but you’re doing nothing with it and just leave it there. You’ll have to do something like:

-- this is just an example
local something
something = true -- no errors

or

-- this is also an example
local something = Instance.new("BodyVelocity")
something.Parent = workspace:FindFirstChildOfClass("Part") -- no errors either

I also mentioned this in one of my posts above.

1 Like
--Variables

--Set the times clicked to 0 before the script
local TimesClicked = 0
--Set a nil variable that we are going to use right after
local theFirstTime

--Function

local function MyInputEvent(input, gameProccess)
--Check if there arent any proccess happening
if gameProccess then return end
if input.KeyCode == Enum.KeyCode.W then
  --Check the ammount of times that was clicked and increase the clicked
   TimesClicked = TimesClicked + 1
   if TimesClicked == 2 then
      --Set back to 0
      TimesClicked = 0
      local theMoment = tick()
      --Get the current time and then subtract with the old time
      if theMoment - TheFirstTime <= 0.8 then
            -- Compare them
            --The code you want
      end
   elseif TimesClicked == 1 then
      --Give the time the first click happened to the nil value that was set up in the start
      TheFirstTime = tick()
   end
end


--Event

game:GetService("UserInputService").InputEnded:Connect(MyInputEvent)
-- Using input ended in case I would only want the code to run when i finish clicking the mouse
7 Likes