Dual Key Input System

Hello, I am attempting to make a system where a user would have to press 2 keys for the operation (Print “test”) to proceed. The E key must be pressed while Left Ctrl is held down for the script to work.

This system works well, however after Left Ctrl is released, it allows E to be pressed one more time before shutting down. How do I prevent this from happening? Thank you.

local key = game:GetService("UserInputService")

local LeftCtrl = false
local EKey = false

key.InputBegan:Connect(function()
	if key:IsKeyDown(Enum.KeyCode.LeftControl) then
		LeftCtrl = true
	else
		LeftCtrl = false
	end	
end)

key.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and LeftCtrl == true and EKey == false then
		print("test")
		EKey = true
		EKey = false
	end
end)

This doesn’t update when the player releases the Ctrl key. You might as well just run the code like this.

key.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E and key:IsKeyDown(Enum.KeyCode.LeftControl) then
		print("test")
	end
end)

Well first first problem is that ur using input began 2 times… that will break the function, and u should instead of using debounces use the InputEnded event

What do you mean? You can connect a single signal to multiple functions. (albeit still unnecessary in this case)

Oh really? Thought that it didnt work but you still should avoid doing that

Here I am overcomplicating things again… This works very well, thank you for your help!

I would recommend adding an elseif to InputBegan to check if the E key is also pressed. You should also add an InputEnded to set those values to false if the key stops being pressed.

In the inputbegan event you should check if both keys are being pressed in a separate if statement just below the first one.

local UserInputService = game:GetService("UserInputService")

local leftControlDown = false
local eDown = false

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		leftControlDown = true
	elseif input.KeyCode == Enum.KeyCode.E then
		eDown = true
	else
		return
	end
	
	if leftControlDown and eDown then
		print("Test")
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		leftControlDown = false
	elseif input.KeyCode == Enum.KeyCode.E then
		eDown = false
	else
		return
	end
end)

If you want it so it’s not reversible like CTRL + C has to be in that order C + CTRL doesn’t work then this code makes it so you need to press leftcontrol down first

local UserInputService = game:GetService("UserInputService")

local leftControlDown = false

UserInputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		leftControlDown = true
	elseif input.KeyCode == Enum.KeyCode.E and leftControlDown then
		print("Test")
	end
end)

UserInputService.InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.LeftControl then
		leftControlDown = false
	end
end)

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.