How to Detect what Device a Player is Using

One important feature is being able to find what device a player is using. For example, you might want touchscreen buttons for cars, or have less advanced GUIs for certain devices. Anyway, this is how you can detect it:

local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")
function getplatform()
    if (GuiService:IsTenFootInterface()) then
        return "Console"
    elseif (UserInputService.TouchEnabled and not UserInputService.MouseEnabled) then
        --touchscreen computers now have touchenabled so make sure to check for lack of mouse too
		--also, not all phones/tablets have accelerometer and/or gyroscope
        local DeviceSize = workspace.CurrentCamera.ViewportSize; 
		if ( DeviceSize.Y > 600 ) then
			return "Mobile (tablet)"
		else
			return "Mobile (phone)"
		end
    else
        return "Desktop"
    end
end

Credit to @Maelstronomer for originally making this script, just I thought I should post it here.

33 Likes

Some phones have super high res displays so this may detect them as tablets

3 Likes

Phones with super high res displays can be technically considered as tablets. Also, there is no need to separate phones and tablets, you can just say Mobile and you will be ok

1 Like

You can just use the UserInputService to see if the player has a keyboard, mouse.etc

1 Like

There is a UserInputService:GetPlatform(), but it is RobloxScriptSecurity, they are planning to change the permission of this to None when they better have an idea of how to secure it, because there used to be a property that you could read on Player called Player::OsPlatform , you cannot read or write to it now.

static Reflection::BoundFuncDesc<UserInputService, UserInputService::Platform(void)> func_getPlatform(&UserInputService::getPlatformLua, "GetPlatform", Security::RobloxScript);
template<>
EnumDesc<UserInputService::Platform>::EnumDesc()
:EnumDescriptor("Platform")
{
    addPair(UserInputService::PLATFORM_WINDOWS, "Windows");
    addPair(UserInputService::PLATFORM_OSX, "OSX");
    addPair(UserInputService::PLATFORM_IOS, "IOS");
    addPair(UserInputService::PLATFORM_ANDROID, "Android");
	addPair(UserInputService::PLATFORM_XBOXONE, "XBoxOne");
	addPair(UserInputService::PLATFORM_PS4, "PS4");
	addPair(UserInputService::PLATFORM_PS3, "PS3");
	addPair(UserInputService::PLATFORM_XBOX360, "XBox360");
	addPair(UserInputService::PLATFORM_WIIU, "WiiU");
    addPair(UserInputService::PLATFORM_NX, "NX");
	addPair(UserInputService::PLATFORM_OUYA, "Ouya");
	addPair(UserInputService::PLATFORM_ANDROIDTV, "AndroidTV");
	addPair(UserInputService::PLATFORM_CHROMECAST, "Chromecast");
	addPair(UserInputService::PLATFORM_LINUX, "Linux");
	addPair(UserInputService::PLATFORM_STEAMOS, "SteamOS");
	addPair(UserInputService::PLATFORM_WEBOS, "WebOS");
	addPair(UserInputService::PLATFORM_DOS, "DOS");
	addPair(UserInputService::PLATFORM_BEOS, "BeOS");
	addPair(UserInputService::PLATFORM_UWP, "UWP");
    addPair(UserInputService::PLATFORM_NONE, "None");
}

typedef enum
{
    PLATFORM_WINDOWS = 0,
    PLATFORM_OSX,
    PLATFORM_IOS,
    PLATFORM_ANDROID,
	PLATFORM_XBOXONE,
	PLATFORM_PS4,
	PLATFORM_PS3,
	PLATFORM_XBOX360,
	PLATFORM_WIIU,
	PLATFORM_NX,
	PLATFORM_OUYA,
	PLATFORM_ANDROIDTV,
	PLATFORM_CHROMECAST,
	PLATFORM_LINUX,
	PLATFORM_STEAMOS,
	PLATFORM_WEBOS,
	PLATFORM_DOS,
	PLATFORM_BEOS,
	PLATFORM_UWP,
    PLATFORM_NONE
} Platform;
// Durango is a C++ SDK for Xbox
UserInputService::Platform UserInputService::getPlatform()
{
#if defined(RBX_PLATFORM_IOS)
return PLATFORM_IOS;
#elif defined(__APPLE__) && !defined(RBX_PLATFORM_IOS)
return PLATFORM_OSX;
#elif defined(RBX_PLATFORM_DURANGO)
return PLATFORM_XBOXONE;
#elif defined(RBX_PLATFORM_UWP)
return PLATFORM_UWP;
#elif defined(RBX_PLATFORM_DURANGO)
return PLATFORM_XBOXONE;
#elif defined(_WIN32) && !defined(RBX_PLATFORM_DURANGO) && !defined(RBX_PLATFORM_UWP)
return PLATFORM_WINDOWS;
#elif defined(__ANDROID__)
return PLATFORM_ANDROID;
#else
return PLATFORM_NONE;
#endif
}
5 Likes

You should probably credit the person who wrote this

2 Likes

I meant to, but I forgot to. It is all updated now.

1 Like

Phones are getting bigger (to the point where some are literally called “phablets”, a portmanteau of phone and tablet), tablets are becoming computers, albeit smaller, you can connect most tablets to a keyboard and mouse and they work, some laptops are touch-screen as well. So how do you detect between a touch-screen laptop and a table hooked up to a computer and mouse? I think better choices are to allow users to choose what display they want (PC, phone, tablet, console), or making your game adapt to the different inputs.

3 Likes