Detect player's device

I need to be able to detect the player’s device for my upcoming game so the UI layout can be updated appropriately. UI positioning that looks good on PC looks bad on mobile, so the UI needs to be re-positioned if the user is on a mobile device. I had no idea how to check this, so I found this script posted on DevForum. It seems to work well, but the only issue is that the mobile user must first press the screen in order for the UI to reposition. So the UI looks one way and then they tap the screen and suddenly the whole user interface shifts to different positions (which can look kind of odd to a player).

Is there anyway to work around this issue? Any help is much appreciated! (Also, is this script a reliable way to tell what device a player is on?)

local UserInputService = game:GetService("UserInputService")

local function checkDeviceType()
    local screenSize = workspace.CurrentCamera.ViewportSize
    if screenSize.X >= 1024 and screenSize.Y >= 768 then
        return "iPad or larger tablet"
    elseif screenSize.X >= 800 and screenSize.Y >= 480 then
        return "Phone with a larger screen"
    else
        return "Smaller phone"
    end
end

UserInputService.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.Touch then
        local deviceType = checkDeviceType()
        print("User is using a " .. deviceType)
    elseif input.UserInputType == Enum.UserInputType.Keyboard then
        print("User is using a keyboard")
    end
end)
local UserInputService = game:GetService("UserInputService")

local function checkDeviceType()
	local screenSize = workspace.CurrentCamera.ViewportSize
	if screenSize.X >= 1024 and screenSize.Y >= 768 then
		return "iPad or larger tablet"
	elseif screenSize.X >= 800 and screenSize.Y >= 480 then
		return "Phone with a larger screen"
	else
		return "Smaller phone"
	end
end

if UserInputService.TouchEnabled and not UserInputService.KeyboardEnabled then
	local deviceType = checkDeviceType()
	print("User is using a " .. deviceType)
elseif UserInputService.KeyboardEnabled then
	print("User is using a keyboard")
end

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.