How can I tell the difference between a mobile and tablet player

The title is self-explanatory.

4 Likes

long answer: you can’t

1 Like

Someone already made a post about it, you can only detect if the player is on either pc or phone/tablet.

2 Likes

Like the previous 2 people mentioned. You can’t. But what’s the difference, why would you need to know, it should be the same

3 Likes

Well, I’m trying to make some kind of ui system but I want to make it close to the jump button and it’s size/position changes depending on if it’s a tablet or mobile

So I want a reliable way to know if it’s a mobile or a tablet

2 Likes

Roblox does not provide us a way to check a user’s device. The answer that’s been adopted by a good number of developers is to keep up to date with the kinds of input a user is passing and modify any Guis according to that input type. See GetLastInputType. You may want to use other members of UserInputService to help out with this as well, just to strengthen the checks.

1 Like

This is my first attempt at detecting a user’s platform. In this LocalScript, only a few UserInputTypes are used.

EDIT: I figured out how to do estimate if a player is using phone or tablet. We simply go by aspect ratio. We use the current ViewportSize and divide X / Y to get our ratio. Many phones hover around 1.5, 1.7, to 2. Tablets range less than 1.5.

EDIT 2: I have a functioning method to grab the user’s device which takes the aspect ratio into account. Here’s the updated script and a video presentation.

EDIT 3: Final edit I apologize, had to fix typos in the script. I really hope this helps someone.

local UserInputService = game:GetService("UserInputService")
local PlayerGui = game:GetService("Players").LocalPlayer.PlayerGui

local text = PlayerGui:WaitForChild("Gui"):WaitForChild("TextLabel")

local devicetable = { -- table of UserInputType Enums to watch
	{"touch",7,{"phone", "tablet"}},
	{"pc",0,1,2,3,4,8},
	{"xbox",12,13,14,15,16,17,18,19},
	{"unknown",22}
}

local device = nil -- device begins unknown

local function checkRatio(ratio, device) -- check the aspect ratio
	if ratio < 1.5 then
		text.Text = device[3][2]
		print(device[3][2]) -- device is more than likely tablet
	else
		if ratio >= 1.5 then
			text.Text = device[3][1]
			print(device[3][1]) -- device is more than likely phone
		end
	end
end

local function checkMobile(device) -- check for tablet or phone using the screen size and orientation
	local orientation = PlayerGui.CurrentScreenOrientation -- get the device orientation Enum
	local viewport = workspace.CurrentCamera.ViewportSize -- prepare to calculate aspect ratio using screen pixels
	if orientation == Enum.ScreenOrientation.LandscapeLeft or orientation == Enum.ScreenOrientation.LandscapeRight then
		local ratio = viewport.X/viewport.Y 
		checkRatio(ratio, device)
	else
		if orientation == Enum.ScreenOrientation.Portrait then
			local ratio = viewport.Y/viewport.X
			checkRatio(ratio, device)
		end
	end
end

local function checkDevice(input, device) -- compare our table with the new input and see if we need to swap tables
	for l, v in ipairs(devicetable) do
		if table.find(v, input) ~= nil then
			if v[1] ~= "touch" then
				device = v -- we found the device
				text.Text = device[1]
				print(device[1]) 				
			else
				device = v
				checkMobile(device)
			end
		end
	end
	if device == nil then
		device = devicetable[4] -- unknown device
		text.Text = device[1]
		print(device[1])
	end
	return device -- we have our assumed device returned
end

local function initialCheck(device) -- alter the checkDevice function for player startup
	local input = UserInputService:GetLastInputType().Value
	device = checkDevice(input, device)  -- UserInputService will determine the input
	return device
end

device = initialCheck(device)

UserInputService.LastInputTypeChanged:Connect(function(lastInputType) -- trigger when new input is detected
	local input = lastInputType.Value
	if input ~= 20 and input ~= 9 then -- exclude regain focus of roblox window or text input
		if device == nil or next(device) and table.find(device, input) == nil then -- trigger only when there's no device yet or the input isn't found in the current device's table of UserInputType Enums.
			device = checkDevice(input, device) -- check for device 
		end
	end
end)

-- expected outcome is a print of the assumed device

```This text will be hidden

The only way you could do that, but it is not 100% certain is by calculating aspectratio of the screen

personally, I found a much simpler way; the jump button size changes depending on if the player is on mobile or tablet, therefore:

local UI_TouchGui = PLY_UIs:WaitForChild("TouchGui")
local TGUI_touchcontrolframe = UI_TouchGui:WaitForChild("TouchControlFrame")
local TGUI_jumpbutton = TGUI_touchcontrolframe:WaitForChild("JumpButton")

if TGUI_jumpbutton.Size == UDim2.new(0, 120, 0, 120) then
	PLY_IsOnTablet = true
end

(I found this solution nearly immediately after I went on devforum, forgot to post it XD)

4 Likes