How to detect double keyboard press

Hey Scripters,
I have ran into a problem which I’m not sure of how to figure out when the user pressed the keycode twice. I have tried to figure it but I struggled to do it.

local StarterGui = game:GetService("StarterGui")
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

local UserInputService = game:GetService("UserInputService")
local player = game.Players.LocalPlayer

local function Equip(input, gameProcessed)
	local num = 1
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.One then
				player.Backpack.ClassicSword.Parent = player.Character
				script.Parent.Frame.Sword.ImageColor3 = Color3.new(0.0862745, 0.0862745, 0.0862745)
			elseif input.KeyCode == Enum.KeyCode.Two then
				player.Backpack.RocketLauncher.Parent = player.Character
				script.Parent.Frame.RocketLauncher.ImageColor3 = Color3.new(0.0862745, 0.0862745, 0.0862745)
			elseif input.KeyCode == Enum.KeyCode.Three then
				player.Backpack.ClassicTrowel.Parent = player.Character
				script.Parent.Frame.ClassicTrowel.ImageColor3 = Color3.new(0.0862745, 0.0862745, 0.0862745)
			elseif input.KeyCode == Enum.KeyCode.Four then
				player.Backpack.ClassicTimebomb.Parent = player.Character
				script.Parent.Frame.ClassicTimebomb.ImageColor3 = Color3.new(0.0862745, 0.0862745, 0.0862745)
			elseif input.KeyCode == Enum.KeyCode.Five then
				player.Backpack.ClassicSuperball.Parent = player.Character
				script.Parent.Frame.ClassicSuperball.ImageColor3 = Color3.new(0.0862745, 0.0862745, 0.0862745)
			elseif input.KeyCode == Enum.KeyCode.Six then
				player.Backpack.ClassicSlingshot.Parent = player.Character	
				script.Parent.Frame.ClassicSlingshot.ImageColor3 = Color3.new(0.0862745, 0.0862745, 0.0862745)
			elseif input.KeyCode == Enum.KeyCode.Seven then
				player.Backpack.ClassicPaintballGun.Parent = player.Character	
				script.Parent.Frame.ClassicPaintballGun.ImageColor3 = Color3.new(0.0862745, 0.0862745, 0.0862745)
			end
	elseif 2 >= num then
		if input.UserInputType == Enum.UserInputType.Keyboard then
			if input.KeyCode == Enum.KeyCode.One then
					player.Character.ClassicSword.Parent = player.Backpack
					script.Parent.Frame.Sword.ImageColor3 = Color3.new(0.945098, 0.945098, 0.945098)
				elseif input.KeyCode == Enum.KeyCode.Two then
					player.Character.RocketLauncher.Parent = player.Backpack
					script.Parent.Frame.Sword.ImageColor3 = Color3.new(0.945098, 0.945098, 0.945098)
				elseif input.KeyCode == Enum.KeyCode.Three then
					player.Character.ClassicSuperball.Parent = player.Backpack
					script.Parent.Frame.Sword.ImageColor3 = Color3.new(0.945098, 0.945098, 0.945098)
				elseif input.KeyCode == Enum.KeyCode.Four then
					player.Character.ClassicSlingshot.Parent = player.Backpack
					script.Parent.Frame.Sword.ImageColor3 = Color3.new(0.945098, 0.945098, 0.945098)
				elseif input.KeyCode == Enum.KeyCode.Five then
					player.Character.ClassicTimebomb.Parent = player.Backpack
					script.Parent.Frame.Sword.ImageColor3 = Color3.new(0.945098, 0.945098, 0.945098)
				elseif input.KeyCode == Enum.KeyCode.Six then
					player.Character.ClassicTrowel.Parent = player.Backpack
					script.Parent.Frame.Sword.ImageColor3 = Color3.new(0.945098, 0.945098, 0.945098)
		end
	 end
		
   end
	
end	
	

if UserInputService.KeyboardEnabled then
	UserInputService.InputBegan:Connect(Equip)
end

I need a little help, thanks!

1 Like

If you mean for something like a double tap to sprint, you can do something like this as an example I threw together:

local userInputService = game:GetService('UserInputService')
local players = game:GetService('Players')
local localPlayer = players.LocalPlayer
local timeDifference = 0.5
local walkSpeed = 16
local runSpeed = 24
local forwardPressed = os.time()

userInputService.InputBegan:Connect(function(input,process)
	if not process and input.KeyCode == Enum.KeyCode.W then
		local current = os.time()
		if current-forwardPressed <= timeDifference then
			localPlayer.Character.Humanoid.WalkSpeed = runSpeed
		end
		forwardPressed = current
	end
end)

userInputService.InputEnded:Connect(function(input,process)
	if not process and input.KeyCode == Enum.KeyCode.W then
		localPlayer.Character.Humanoid.WalkSpeed = walkSpeed
	end
end)

You could have a variable, lets say ‘KeyboardPresses’ (local KeyboardPresses = 0 )

And for each time a player has pressed any/specific button on your keyboard, it’d add +1 to that variable

You could check whenever that variable equals to 2, if it is, do something.

If you mean - double press on the same button then :
You could check if the playe has already pressed on a certain button, and if he pressed again, add to your number variable + 1 or just print something if he really pressed twice on the same button.

Yes that was what I was trying to accomplish, but I couldn’t figure out how to do it.

Sorry for not being clear, I am making a hotbar system and I couldn’t figure out how to detect the second press to do the opposite which is unequiping.

1 Like

I assume you want something like a toggle system, this is very easy to implement. A quick implementation of mine:

local userInputService = game:GetService("UserInputService");

local function equip()
	print("Equip code running");
end

local function unequip()
	print("Unequip code running");
end

local toggle = false;

userInputService.InputBegan:Connect(function(input, processed)
	if (processed) then return; end
	if (input.KeyCode ~= Enum.KeyCode.E) then return; end
	
	toggle = not toggle;
	
	if toggle then
		equip();
	else
		unequip();
	end
end);

image