PcoWSkbVqDnWTu_dm2ix
We use cookies on this site to enhance your user experience
Collapse Sidebar

IsNavigationGamepad

This function returns true if the specified Enum/UserInputType Gamepad is allowed to control Navigation and Selection GuiObject|GUIs.

If you want to set a navigation gamepad, you can use UserInputService/SetNavigationGamepad. You can also use UserInputService/GetNavigationGamepads to get a list of all navigation gamepads.

For example, the code below checks if the gamepad1 is as a navigation gamepad:

local userInputService = game:GetService("UserInputService")

if (userInputService:IsNavigationGamepad(UserInputType.Gamepad1) then
	print("Gamepad is a navigation gamepad!")
else
	print("Gamepad is not a navigation gamepad!")
end

A list of all connected gamepads, regardless of navigation can be retrieved using`UserInput/GetConnectedGamepads.

Since UserInputService is client-side only, this function can only be used in a LocalScript.

See this page for articles on cross-platform development.

See also

  • UserInputService/GamepadConnected
  • UserInputService/GamepadDisconnected
  • UserInputService/GetConnectedGamepads
  • UserInputService/GetNavigationGamepads
  • UserInputService/SetNavigationGamepad
  • UserInputService/IsGamepadButtonDown
  • UserInputService/GetSupportedGamepadKeyCodes
  • UserInputService/GetGamepadState
  • UserInputService/GetGamepadConnected
  • UserInputService/GamepadSupports
  • UserInputService/GamepadEnabled

Parameters

Name Type Default Description

The Enum/UserInputType of the specified gamepad

Returns

Return Type Summary

Whether the specified gamepad is a navigation gamepad


Code Samples


How to Use the Right Gamepad for Input

The following code sample is used in the default gamepad script, so if you are using the default controls you do not need to worry about this. If you are creating a custom script for handling gamepad controls, this is a good template for retrieving which Enum/UserInputType|gamepad enum you should use as the primary gamepad controller.

This code looks for the lowest numbered UserInputService/GetNavigationGamepad|navigation gamepad, and if no navigation gamepad is found, finds the lowest numbered UserInputService/GetConnectedGamepad|connected gamepad. If there is no connected gamepad, we default to gamepad1. This ensures at least some gamepad will be able to do controls.

local UserInputService = game:GetService("UserInputService")

local activeGamepad = nil
function GetActiveGamepad()
	local activateGamepad = nil
	local navigationGamepads = {}
	
	navigationGamepads = UserInputService:GetNavigationGamepads()
	
	if #navigationGamepads > 1 then
		for i = 1, #navigationGamepads do
			if activateGamepad == nil then
				activateGamepad = navigationGamepads[i]
			elseif navigationGamepads[i].Value < activateGamepad.Value then
				activateGamepad = navigationGamepads[i]
			end
		end
	else
		local connectedGamepads = {}
		
		connectedGamepads = UserInputService:GetConnectedGamepads()
		
		if #connectedGamepads > 0 then
			for i = 1, #connectedGamepads do
				if activateGamepad == nil then
					activateGamepad = connectedGamepads[i]
				elseif connectedGamepads[i].Value < activateGamepad.Value then
					activateGamepad = connectedGamepads[i]
				end
			end
		end
		if activateGamepad == nil then -- nothing is connected, at least set up for gamepad1
			activateGamepad = Enum.UserInputType.Gamepad1
		end
	end

	return activateGamepad
end

if UserInputService.GamepadEnabled then
	activeGamepad = GetActiveGamepad()
end