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

How do I activate a function when a key is tapped twice?(UNANSWERED)

Asked by 10 years ago

One example: Double jumping. What I'm trying to make is when you press "SPACE" twice, you get thrusted fowards.

p = script.Parent.Parent
repeat wait() until p.Character
c = p.Character
print("HELLO")
mouse = p:GetMouse()
ticknow = nil
tickaft = nil
mouse.KeyDown:connect(
function(key)
    key = key:lower()
    if key:byte() == 32 and not ticknow then
        ticknow = time()
        print("HALLO")
    elseif key:byte() == 32 and ticknow then
        print("HERRO")
        tickaft = time()
        if tickaft - ticknow < 1 then
            print("DOUBLE DUH STOOF")
            tickaft = nil
            ticknow =nil
        end
    end
end)

Regardless of what I do, it either prints out the double function or gets stuck on "HERRO"

2 answers

Log in to vote
0
Answered by
Destrings 406 Moderation Voter
10 years ago

This is pretty straight forward, the basic idea is to check the time between two presses and see if it is lower than a certain value

mouse = game.Players.LocalPlayer:GetMouse()
local last = tick()

mouse.KeyDown:connect(function(key)
    local cur = tick()
    if key == " " then
        if cur-last < 0.3 then
            print("Doble taps")
        end
    end
    last = cur
end)
Ad
Log in to vote
0
Answered by 10 years ago

Whenever I need something to happen after something happens, I just set it up with an anonymous function firing after the first time it happens, then either an until loop that stops when it happens again or another anonymous function that fires when it happens again.

0
Yes, as you can see, I can do that just fine, but I need to be able to check the time difference between activation. xolbStudios 127 — 10y

Answer this question