Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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)

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.

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.