Problems with InputBegan in Connections vs UserInputService

Hello folks,

I’m currently working on Gamepad support with GUI elements. I’m having issues detecting input with the .InputBegan connection for said elements. Only the A button is being detected. However, I’m having NO issues with using the UserInputService to detect the same input.

Let me demonstrate with two code blocks and their respective outputs:

function testButton(input)
	if(input.KeyCode == Enum.KeyCode.ButtonA)then
		print("A Button")
		return false
	elseif(input.KeyCode == Enum.KeyCode.ButtonB)then
		print("B Button")
		return false
	else
		return false
	end
end
script.Parent.Button.InputBegan:Connect(testButton)

The respective output:

-- *i push the A Button*
A Button
-- *i push the B button*
--nothing happens

And now here’s an example using the UserInput Service

local userInputService = game:GetService("UserInputService")

userInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonA then
			print("A Button!")
		elseif input.KeyCode == Enum.KeyCode.ButtonB then
			print("B Button!")
--any other desired buttons, they all work!
		end
	end
end)

The respective output:

-- *i push the A Button*
A Button!
-- *i push the B button*
B Button!

--i can push any button here with the proper keycode statement and a get a response

There must be something simple I’m overlooking. Any help is appreciated!

**addendum: A workaround would be to fire the InputBegan connection from the UserInputService. Determining the proper gui element is easy, with GuiService! But I’m unsure how to trigger that event manually. Insight on either problem is appreciated!

Well yes, this works. But this is just the same result as the UserInputService function I originally posted.

I’m trying to get InputBegan for a specific Gui Element to fire all Gamepad Buttons. Once again, right now it only fires when ‘A’ is pressed on the Gamepad.

UserInputService works for all buttons, but it universally fires.

I’ve designed a shoddy workaround…
Have the UserInputService detect all Gamepad input and then toggle the Selected value of the appropriate GUI element. I can detect this with the .Changed connection and thus, can fire the element-specific function.

Take a look, I don’t like it though:

function testButton(prop)
	if(prop=="Selected")then
		print"Toggled Select"
		if(script.Parent.Selected)then
			print"Selected is true"
			--button logic etc
		end
	end
end
script.Parent.Button.Changed:Connect(testButton)

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

userInputService.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		if input.KeyCode == Enum.KeyCode.ButtonA then
			if(GuiService.SelectedObject)then
				print("UserInputService A Button!")
				GuiService.SelectedObject.Selected = true
			end
		elseif input.KeyCode == Enum.KeyCode.ButtonB then
			print("UserInputService B Button!")
		end
	end
end)

So, sure this works. It completely circumnavigates InputBegan on GuiElements for Gamepad Input (which I’d rather not do, but I can’t get it to detect!)…

But, it might have extraneous cases… if Selected were to be toggled by another means. I couldn’t find anything in a quick documentation look. Does anybody know, does Selected ever get toggled? Quick testing seemed to conclude it doesn’t

1 Like