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
9 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

Input.InputBegan:connect(function(KeyInput)
    if KeyInput.KeyCode == Enum.KeyCode.W then
        Character.Humanoid.Jump = true
         if KeyInput.KeyCode == Enum.KeyCode.W then
            Character.Torso.Velocity = Vector3.new(0, 60, 0)
        end
    end
end)

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
9 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

--Last time the button was pressed
local lastTime = tick()

Input.InputBegan:connect(function(KeyInput)
    --Check the keycode
    if KeyInput.KeyCode == Enum.KeyCode.W then
        --Get tick() time
        local now = tick()
        --compare tick()'s
        local difference = (now - lastTime)
        --Check if difference is less than a second
        if difference <= 1 then
            --if so, enable double jump
            Character.Torso.Velocity = Vector3.new(0, 60, 0)
        else
            --Otherwise, normal jump.
                Character.Humanoid.Jump = true
        end
    end
end)
0
good explanation! Grazer022 128 — 4y
Ad

Answer this question