UserInputService.InputBegan not working for gamepad analog sticks

I was looking to change the text of a button to say which key should be pressed to open a UI, depending on the input type. It works fine with keyboard and gamepad buttons, but nothing happens when I tilt the control sticks and there is nothing in output. Why?

Here’s my script: (a local script inside a button in ScreenGUI)

local inputServ = game:GetService("UserInputService")
local gamepadServ = game:GetService("GamepadService")

local defaultString = script.Parent.Text

function showHints()
	script.Parent.Parent.HintContainer.Hints.Visible = not script.Parent.Parent.HintContainer.Hints.Visible
	
	script.Parent.Parent.ModalButton.Visible = not script.Parent.Parent.ModalButton.Visible
	script.Parent.Parent.ModalButton.Active = not script.Parent.Parent.ModalButton.Active
	
	if script.Parent.Parent.HintContainer.Hints.Visible then
		gamepadServ:EnableGamepadCursor(script.Parent.Parent.HintContainer.Hints)
	else
		gamepadServ:DisableGamepadCursor()
	end
	
end

script.Parent.Activated:Connect(showHints)

inputServ.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Touch then
		script.Parent.Text = defaultString
	end
	
	if input.UserInputType == Enum.UserInputType.Keyboard
		or input.UserInputType == Enum.UserInputType.MouseButton1
		or input.UserInputType == Enum.UserInputType.MouseButton2
		or input.UserInputType == Enum.UserInputType.MouseButton3
		or input.UserInputType == Enum.UserInputType.MouseWheel
		or input.UserInputType == Enum.UserInputType.MouseMovement
	then
		script.Parent.Text = defaultString.."(H)"
		if input.KeyCode == Enum.KeyCode.H then
			showHints()
		end
	end
	
	if input.UserInputType == Enum.UserInputType.Gamepad1 then
		script.Parent.Text = defaultString.."(Y/Triangle)"
		if input.KeyCode == Enum.KeyCode.ButtonY then
			showHints()
		end
	end
end)
1 Like

to detect joystick input, you have to use UserInputService.InputChanged
so it’d be something like this:

inputServ.InputChanged:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.Thumbstick1 or input.KeyCode == Enum.KeyCode.Thumbstick2 then
		-- code here
	end
end)
1 Like

Thank you. This is exactly what I’ve been looking for.

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