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

Double Tap with UserInputService?

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

✓ Accepted answer

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)

1 comment

0

good explanation!

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