How do i make something if you doubletap a key

I am making a main menu and i want to make it reopen after doubletapping a key.

What is the issue? It prints it always when you press the key not double tapping.

UIS.InputBegan:Connect(function(input, gameProcessed)
	if input.KeyCode == Enum.KeyCode.M then 
		if not gameProcessed then
	       	if not lastMPress then
				lastMPress = true
				wait(0.25)
				lastMPress = false
			else
				print("doubletapped")
			end
		end
	end	
end)
4 Likes

Btw, gameProcessed if statement belongs first to be checked.

Also, this prints double tap everytime the button is pressed in a 0.25 second time frame rather than double tap, you need to reset lastMPress on the second tap.

I have a double press key for my main menu. The idea behind determining when someone pressed a key twice is to determine when they last pressed the key and then act upon that value. This typically should, to retain the spirit of the action, be held in a small time window.

local EXIT_THRESHOLD = 0.25

local lastMPress = 0

UIS.InputBegan:Connect(function (inputObject, gameProcessedEvent)
    if gameProcessedEvent then return end

    if inputObject.KeyCode == Enum.KeyCode.M then
        local currentTime = tick()
        if currentTime - lastMPress <= EXIT_THRESHOLD then
            doublePressedMAction()
        else
            lastMPress = currentTime
        end
    end
end)
6 Likes
local Sens = 0.12
local LastPressed = nil
game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.M then
		local Trig = false
		if LastPressed ~= nil then
			if tick() - LastPressed <= Sens then
				print("Open Menu")
				Trig = true
			end
		end
		if not Trig then
			LastPressed = tick()
		end
	end 
end)
1 Like