Why don't the controller keycodes register?

I’m trying to get a signal to fire whenever the E key or the X button on a gamepad is pressed but it only seems to function when the E key is pressed. I tested it on studio and on an xbox one, still the same issue.

local UIS = game:GetService("UserInputService")
local keyE = Enum.KeyCode.E
local buttonX = Enum.KeyCode.ButtonX

local function WasItInteract()
	return UIS:IsKeyDown(keyE) or UIS:IsKeyDown(buttonX)
end

UIS.InputBegan:Connect(function(input)
	if WasItInteract() then
		print("Interact pressed")
	end
end)

Try printing input.KeyCode before the if WasItInteract() then. Then tell us what appears on the output

It prints Enum.KeyCode.ButtonX, weird

What if you sent the input.KeyCode then you compare both of them

local UIS = game:GetService("UserInputService")
local keyE = Enum.KeyCode.E
local buttonX = Enum.KeyCode.ButtonX

local function WasItInteract(key)
	return key == keyE or key == buttonX
end

UIS.InputBegan:Connect(function(input)
	if WasItInteract(input.KeyCode) then
		print("Interact pressed")
	end
end)
2 Likes

You may also want to include the processed parameter in order to ignore processed inputs (inputs related to Roblox’s core gui).