Is there any way to detect a player if the player using mobile/pc?

I think that I need local script the connected to a remote events but I already created it but doesn’t work can someone help me with the script?

Script:

RemoteEvent.OnServerEvent:Connect(function(player, device)
    if device == "Console" then
      GuiClone.Frame.IconHolder.Console.Visible = true
    elseif device == "Computer" then
      GuiClone.Frame.IconHolder.Computer.Visible = true
    elseif device == "Mobile" then
      GuiClone.Frame.IconHolder.Mobile.Visible = true
    end
end)

Local script:

local UserInputService = game:GetService("UserInputService")
local deviceCheck = game.ReplicatedStorage:FindFirstChild("deviceCheck")

if UserInputService.GamepadEnabled then
	deviceCheck:FireServer("Console")
elseif UserInputService.KeyboardEnabled and UserInputService.MouseEnabled then
  deviceCheck:FireServer("Computer")
elseif UserInputService.TouchEnabled then
  deviceCheck:FireServer("Mobile")
end

This might be happening because the client runs FireServer before your server code is ready to listen.

One way you can make sure is to have your server code enable the client code after it calls :Connect(). You can have the LocalScript start as Disabled, but then have the server code set Disabled to false when the server code has finished running and is ready.

This is just a guess though.

Also, in this particular case, it’s probably fine to do this in your LocalScript, it’s a good exception since GUIs are mostly client-side things that only your one player will interact with. You probably don’t need to use remotes to change the Visibility on the server for GUIs, since the GUIs are client-side, you just have to keep in mind that the Visible property won’t be true in your server code.


Also, while it’s not the end of the world, you should avoid trying to detect the player’s device in the future. Roblox intentionally doesn’t tell you the player’s device, as really, you want your UI to work on any device with any methods of input. It’s definitely a lot harder to design UI this way but it’s something you should strive to get good at learning for the best experience.

This is important, because a computer with a touch screen isn’t a mobile device. A computer with a gamepad connected isn’t a console. A phone with a keyboard and mouse plugged in isn’t a PC (USB OTG cables are a thing!). And a console with a keyboard and mouse plugged in isn’t either.

1 Like

Any solution how do I fix it because I really want to make that

What do you mean by that? I dont really understand

You can test what device they are on by this:

local uis = game:GetService(“UserInputService”)

if uis.KeyboardEnabled then — if the player is on computer—
    print(“Keyboard”)
elseif uis.TouchEnabled then — if player is on mobile —
    print(“Mobile”)
elseif uis.GamepadEnabled then — if player is on console —
    print(“Console”)
end

Hope this helps, if you need anything more let me know :slight_smile:

But I want to connect that in the event

From the code provided, it seems like what you really want to do is display an icon to broadcast what input type a player is using - not their device. To quote Hexcede, earlier in this thread…

As for how to go about sending a player’s input method to the server, connect UserInputService.LastInputTypeChanged to a function that uses the player’s last UserInputType to determine whether they’re using a KB+M, a controller, or a touchscreen (perhaps by comparing it against a table). Then, send that information to the server - which then updates the GUI accordingly.