How can i get the controller axis

as you know controllers have two buttons L and R but in roblox i dont find anything to get the axis of them i want to make game with them but how?

thank you for readinng! :smiley:

Working with gamepads is a but more complicated than mouse and keyboard. Here’s some example code to show you how it can be done:


local InputS = game:GetService("UserInputService")
local RunS = game:GetService("RunService")

function find_if(t, predicate)
	for key, value in pairs(t) do
		if predicate(value) then
			return key, value
		end
	end
end

function get_gamepad_button_axis(gamepad, button)
	local gamepad_state = InputS:GetGamepadState(Enum.UserInputType.Gamepad1)
	local _, button_input_object = find_if(gamepad_state, function(input_object)
		return input_object.KeyCode == button
	end)

	if button_input_object then
		return button_input_object.Position.Z
	else
		warn( ("Couldn't find gamepad state for button %s, gamepad %s"):format(tostring(button),  tostring(gamepad)) )
	end
end

RunS.RenderStepped:Connect(function()
	local r2_axis = get_gamepad_button_axis(Enum.UserInputType.Gamepad1, Enum.KeyCode.ButtonR2)
	print(r2_axis)
end)

Let me know if you have any questions :slight_smile:

i mean this buttons:
image

Maybe you could refer to the posts on here if that helps your case at all?

1 Like

Do you mean moving the thumb sticks or PRESSING the thumb sticks? Pressing them is just buttons L3 and R3

1 Like