Keyinput not being detected

I used the solution to this devforum post to detect if a player has made a key combination. The code worked. Although, switched it to leftcontrol and L and it didnt work, i also tried rightcontrol and L. Then, when I tried R and L, it worked. And, when I tried Shift and L it worked.

So how come it is not detecting it whenever I use one of the control keys?
I assume this is yet another Roblox bug but I dont want to jump to conclusions.

The solution you provided is harder to read and, possibly in the future when your code grows, harder to maintain. Use this method instead:

local UIS = game:GetService("UserInputService")
local keys = {R = false, LeftControl = false} -- Key names should reflect what you see when typing "Enum.KeyCode." (e.g. LeftShift, as in Enum.KeyCode.LeftShift)

local function inputProcessor()
	-- Other code that require listening to the input can be placed here.
	if keys.R and keys.LeftControl then
		print("It works.")
	end
end

local function manageKey(input : InputObject, gameProcessedEvent)
	if (keys[input.KeyCode.Name] == nil) then
		return -- If the keycode is not in the keys table, ignore.
	end

	keys[input.KeyCode.Name] = input.UserInputState == Enum.UserInputState.Begin
	
	inputProcessor()
end

UIS.InputBegan:Connect(manageKey)
UIS.InputEnded:Connect(manageKey)
1 Like

Thank you for the new code. I still do wonder though if it was a bug causing my problem.

You’re most likely not setting the code up properly, for the code detects if the user pressed “R” and check if “LeftControl” is down, or if the user pressed “LeftControl” while “R” is down. Two similar sounding scenarios, but both work differently.

(input.KeyCode == Enum.KeyCode.LeftControl -- If user pressed LeftCtrl
	and UIS:IsKeyDown(Enum.KeyCode.R)) -- and R is down,
or (input.KeyCode == Enum.KeyCode.R -- or user pressed R
	and UIS:IsKeyDown(Enum.KeyCode.LeftControl)) -- and LeftCtrl is down.

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