Code does not recognize device accurately

I got these blocks of code from other threads and tried to make it so an image of the device the player is currently playing on will appear. But I joined the game on my phone and it shows the image of the monitor (PC) and prints “PC” and should print “Mobile” and show a phone image in the dev console. Any help is appreciated thanks

local Players = game:GetService("Players")
local GuiService = game:GetService("GuiService")
local UserInputService = game:GetService("UserInputService")

function getplatform()
	if (GuiService:IsTenFootInterface()) then
		NameTagClone.Frame.Console.Visible = true
		print("Console")
		return "Console"
	elseif (UserInputService.TouchEnabled and not UserInputService.MouseEnabled) then
		local DeviceSize = workspace.CurrentCamera.ViewportSize; 
		if ( DeviceSize.Y > 600 ) then
			NameTagClone.Frame.Tablet.Visible = true
			print("Tablet")
			return "Mobile (tablet)"
		else
			NameTagClone.Frame.Mobile.Visible = true
			print("Mobile")
			return "Mobile (phone)"
		end
	else
		NameTagClone.Frame.PC.Visible = true
		print("PC")
		return "Desktop"
	end
end

local function CharacterAdded(char)
	--Character added code VVV
	getplatform()
end






local function PlayerAdded(player)
	player.CharacterAdded:Connect(CharacterAdded)

	local char = player.Character
	if char then
		CharacterAdded(char)
	end
end

game.Players.PlayerAdded:Connect(PlayerAdded)

for i,v in pairs(game.Players:GetPlayers()) do
	PlayerAdded(v)
end

I don’t know if this is the issue, but one difference I see in this code that I have never seen is:

and not UserInputService.MouseEnabled

I think its to see if it’s not just a laptop with a touchscreen screen to make sure if it’s a phone/tablet but just assumption since that was mentioned in the original post of that function

This is going to sound silly, but are you using a server or client script?

1 Like

I’m using a server script in StarterCharacterScripts and there are the images and a rank tag inside of it
frame (Nametag script is where everything is)

What I do in my games is this simple check in a Local Script under StarterPlayer/StarterPlayerScripts

local UserInputService = game:GetService("UserInputService")

if UserInputService.TouchEnabled then
	print("Is on mobile!")
elseif UserInputService.GamepadEnabled then
	print("Is on Xbox!")
else
	print("Is on PC")
end

You need a client script to use UserInputService

1 Like

UserInputService doesn’t work on the server, do it on the client.

1 Like

This is what I do, but it might be too simple depending on what the poster needs done

1 Like

Okay thank you both for letting me know this. I did not realize that but now I will have a new thing to check my code when it isn’t working to see if it works only on a client or server script.

1 Like