Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Double Tap with UserInputService?

Asked by
Hero_ic 502 Moderation Voter
10 years ago

I'm trying to make a double jump script I know how to make the player jump twice but I do not know how to make it work by double tapping w :P

My attempt below V

1Input.InputBegan:connect(function(KeyInput)
2    if KeyInput.KeyCode == Enum.KeyCode.W then
3        Character.Humanoid.Jump = true
4         if KeyInput.KeyCode == Enum.KeyCode.W then
5            Character.Torso.Velocity = Vector3.new(0, 60, 0)
6        end
7    end
8end)

But that does not work :/ Can someone show me how to do this?

Thanks ~KiHeros

1 answer

Log in to vote
5
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
10 years ago

Your Problem

You're not thinking practically. You're thinking that by putting 2 if statements then it will check if they hit the button 2 times? It checks twice, if they hit the button. Not checks if they hit the button twice. Understand?


What You Need To Do

You need to take advantage of the tick function.

  • tick function

returns the amount of time, in seconds, has passed since the epoch, or January 1, 1970.

By using this function you can get the time before the jump button was pressed, and after. If the difference between the two is less than a set time.. then activate a double jump.


Code

01--Last time the button was pressed
02local lastTime = tick()
03 
04Input.InputBegan:connect(function(KeyInput)
05    --Check the keycode
06    if KeyInput.KeyCode == Enum.KeyCode.W then
07        --Get tick() time
08        local now = tick()
09        --compare tick()'s
10        local difference = (now - lastTime)
11        --Check if difference is less than a second
12        if difference <= 1 then
13            --if so, enable double jump
14            Character.Torso.Velocity = Vector3.new(0, 60, 0)
15        else
16            --Otherwise, normal jump.
17                Character.Humanoid.Jump = true
18        end
19    end
20end)
0
good explanation! Grazer022 128 — 4y
Ad

Answer this question