Script won't detect the platform and put the textable to "Mobile"

I am trying to make a script so that if someone is on mobile the GUI above their head will say “mobile” and it always thinks I am on PC or have keyboard enabled. I am quite new to scripting so it may just be a rookie error, if you need any other information/screenshots I will see if I can get them for you.

This is my script:

local UserInputService = game:GetService("UserInputService")
local mobile = UserInputService.TouchEnabled
local touchscreen = UserInputService.KeyboardEnabled and UserInputService.TouchEnabled

if mobile == true then
		game.Players.PlayerAdded:Connect(function(player)
			player.CharacterAdded:Connect(function(char)
				if char:FindFirstChild("Head") and player then
					local clone = script.GUI:Clone()
					clone.Parent = char:FindFirstChild("Head")
					-----------------------------------------
					clone.TextLabel.Text = "Mobile"
				clone.TextLabel.TextColor3 = Color3.fromRGB(53, 107, 165)
				end
		end)
	end)
	
elseif touchscreen == true then
	game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function(char)
			if char:FindFirstChild("Head") and player then
				local clone = script.GUI:Clone()
				clone.Parent = char:FindFirstChild("Head")
				-----------------------------------------
				clone.TextLabel.Text = " "
				clone.TextLabel.TextColor3 = Color3.fromRGB(165, 22, 146)
			end
		end)
	end)
	
	game.Players.PlayerAdded:Connect(function(player)
		player.CharacterAdded:Connect(function(char)
			if char:FindFirstChild("Head") and player then
				local clone = script.Name:Clone()
				clone.Parent = char:FindFirstChild("Head")
				-----------------------------------------
				clone.TextLabel.Text = player.name
				clone.TextLabel.TextColor3 = Color3.fromRGB(165, 0, 0)

			end
		end)
	end)
end


The images are of what it looks like on mobile and the script with the GUIs in.

image

Your connecting a lot of PlayerAdded, so if your on a computer then all people would display " "

I would suggest putting a localscript in StarterCharacterScripts, then test if your on mobile or something else, send a remote event to the server with what it is and then the server adds the gui to the player head.

something like
client

local UserInputService = game:GetService("UserInputService")
local isMobile = UserInputService.TouchEnabled
local isComputer = UserInputService.KeyboardEnabled
local isConsole = UserInputService.GamepadEnabled
local remoteEvent = --Put Remote Event Here

if isMobile then
	remoteEvent:FireServer("Mobile")
elseif isComputer then
	remoteEvent:FireServer("Computer")
elseif isConsole then
	remoteEvent:FireServer("Console")
end

and the server script

local remoteEvent = --Put event here
local originalGui = --Put Gui Here

remoteEvent.OnServerEvent:Connect(function(player, Platform)

	local Clone = originalGui:Clone()
	Clone.Parent = player.Character.Head
	Clone.TextLabel.Text = Platform
end)

I haven’t used remote events so would I add a script and put some code that would change the TextLable?

User input service must be used in local scripts, The server cannot detect the platform of the person. The person needs to tell the script what platform it is on via a remote event. Also if you use a localscript then all the guis will not be able to be seen by other people

1 Like

Remote events work best in Replicated storage, and if you would want to be organized you can make your own little folder for them

Image from Gyazo

Here is the article that explains remote events

It worked, thanks. I Will read about remote events so I can use them more.