Is this the best way to determine the device?

Hello, I need to determine the exact device for probably one of my biggest projects on Roblox.

Is this the best way to determine the device’s idiom? (phone, tablet, computer, console)

Thanks in advance!

local function getDeviceIdiom()
	local userInputService = game:GetService("UserInputService")

	if userInputService.TouchEnabled and not userInputService.KeyboardEnabled and not userInputService.MouseEnabled then
		local screenSize = workspace.CurrentCamera.ViewportSize
		local smallestDimension = math.min(screenSize.X, screenSize.Y)
		local aspectRatio = screenSize.X / screenSize.Y

		if smallestDimension <= 480 or aspectRatio <= 0.75 then
			return "phone"
		elseif smallestDimension <= 768 or aspectRatio <= 1.5 then
			return "tablet"
		end
	elseif userInputService.GamepadEnabled then
		return "console"
	else
		return "computer"
	end
end
1 Like

if userInputService.TouchEnabled and not userInputService.KeyboardEnabled and not userInputService.MouseEnabled then could be achieved by a computer with a touchscreen device with its keyboard and mouse disconnected, and in addition, there are some cases where the resolutions or aspect ratios won’t match and your function will return no device type.

elseif userInputService.GamepadEnabled then would also return “console” on any device that has a game pad connected. You should use GuiService:IsTenFootInterface() instead.

But other than that, this code could work.

1 Like

I may want the controller to be supported by the computer and mobile device tbh. That is why I dont check that. Its mainly the tablet and phone I want to check.

1 Like

Well, I don’t know what you’re trying to achieve, but if it makes sense to you, then do as you will. But if you want to accurately detect a console and not a computer with a gamepad connected, you should use the ten foot interface function.

Im trying to achieve the best way to determine which UI to use.

I mainly want to check if the tablet and phone would have false positives.

1 Like

If the purpose is UI then your original code will work, just fix the possibility of no device returning if the resolution doesn’t match the two cases you provided.

1 Like