Using UIS to determine what platform a player is on?

Hi, so I’m not exactly sure what I’d need to write up in order to find out whether the player is on controller, PC, or mobile devices.

I assume this is just a few lines of code to figure out, but I’m not exactly sure what to write. d:

Thanks!

1 Like

This is some code that’s quite popular around the community, may help you. It’s only an initialization though, you’d probably want to use a LastInputTypeChanged event.

function getPlatform()
    if (game:GetService("GuiService"):IsTenFootInterface()) then
        return "Console"
    elseif (game:GetService("UserInputService").TouchEnabled and not game:GetService("UserInputService").MouseEnabled) then
        return "Mobile"
    else
        return "Desktop"
    end
end
7 Likes

It is impossible to detect with absolute certainty what device a player is using, and for good reason…

Instead of caring about what device a player is using, you should care about their input type and screen size. In the end, the only real use of knowing a player’s platform would be to derive their input type and screen size from it to appropriately change GUI and gameplay. However, a player’s device may not always reflect their input type (consider a desktop player using a touchscreen monitor, or a console player using a connected keyboard and mouse). It therefore makes more sense to get a player’s input type and screen size directly.

Input type detector module source
-- dependencies:
local userInputService = game:GetService("UserInputService")

-- enums:
local inputTypes = {
	KeyboardAndMouse = 1,
	Gamepad = 2,
	Touch = 3,
}

-- data:
local inputTypeToEnumMap = {
	[Enum.UserInputType.MouseButton1] = inputTypes.KeyboardAndMouse,
	[Enum.UserInputType.MouseButton2] = inputTypes.KeyboardAndMouse,
	[Enum.UserInputType.MouseButton3] = inputTypes.KeyboardAndMouse,
	[Enum.UserInputType.MouseWheel] = inputTypes.KeyboardAndMouse,
	[Enum.UserInputType.MouseMovement] = inputTypes.KeyboardAndMouse,
	[Enum.UserInputType.Keyboard] = inputTypes.KeyboardAndMouse,
	[Enum.UserInputType.Touch] = inputTypes.Touch,
	[Enum.UserInputType.Gamepad1] = inputTypes.Gamepad,
	[Enum.UserInputType.Gamepad2] = inputTypes.Gamepad,
	[Enum.UserInputType.Gamepad3] = inputTypes.Gamepad,
	[Enum.UserInputType.Gamepad4] = inputTypes.Gamepad,
	[Enum.UserInputType.Gamepad5] = inputTypes.Gamepad,
	[Enum.UserInputType.Gamepad6] = inputTypes.Gamepad,
	[Enum.UserInputType.Gamepad7] = inputTypes.Gamepad,
	[Enum.UserInputType.Gamepad8] = inputTypes.Gamepad,
}
local inputType = inputTypeToEnumMap[userInputService:GetLastInputType()] or inputTypes.KeyboardAndMouse
local inputTypeChangedBindable = Instance.new("BindableEvent")


-- InputTypeDetector:
local module = {}

function module.GetInputType()
	return inputType
end

module.InputTypeChanged = inputTypeChangedBindable.Event

module.InputTypes = inputTypes


-- main:
userInputService.LastInputTypeChanged:Connect(function(newInputType)
	local enum = inputTypeToEnumMap[newInputType]
	if enum == nil then return end
	if enum == inputType then return end
	inputType = enum
	inputTypeChangedBindable:Fire(inputType)
end)

return module
Input type detector in use
local inputTypeDetector = require(game:GetService("ReplicatedStorage").Modules.InputTypeDetector)
local inputTypes = inputTypeDetector.InputTypes

local function updateGameplayGuiForInputType(inputType)
    if inputType == inputTypes.KeyboardAndMouse then
        -- ...
    elseif inputType == inputTypes.Touch then
        -- ...
    elseif inputType == inputTypes.Gamepad then
        -- ...
    end
end

updateGameplayGuiForInputType(inputTypeDetector.GetInputType())
inputTypeDetector.InputTypeChanged:Connect(updateGameplayGuiForInputType)
6 Likes

Be really careful when using this type of code. Using these assume platforms can cause problems because supported inputs can change. Such as:

  • Xbox supports keyboards (not sure if Roblox supports this).
  • Android supports mice, keyboards, and gamepads. iOS supports keyboards.
  • Winodws and Linux support touchscreens.

Dandystan’s advise is also good because sometimes the initial check will fail if multiple input devices are selected. I was attempting to play a game a few months ago using the Windows 10 Store version that accidentally thought I had a touch screen, and I had to quit after a few minutes after realizing I couldn’t interact with objects since they only had tapping supported.

5 Likes