.OnClientEvent not working

As the title says, .OnClientEvent is not working for my script to detect what device each player is on, and then change the text to either Click or Tap to open

Server script in SSS


while true do
	wait(1)
	if game:GetService("UserInputService").TouchEnabled and game:GetService("UserInputService").MouseEnabled == false then
		game.ReplicatedStorage.IsMobile:FireAllClients()
		print("Mobile")
		



while true do
	wait(1)
	if game:GetService("UserInputService").TouchEnabled == false and game:GetService("UserInputService").MouseEnabled == true then
		game.ReplicatedStorage:WaitForChild("IsPC"):FireAllClients()
		print("PC")
		

	end
end




	end
end



Local script in Billboard Gui Text Label

while true do
	wait(1)
	game.ReplicatedStorage.IsMobile.OnClientEvent:Connect(function(Player)

		script.Parent.Text = "Tap to Open!"
		print("Mobile2")
	end)
end

while true do
	wait(1)
	game.ReplicatedStorage.IsPC.OnClientEvent:Connect(function()
	script.Parent.Text = "Click to Open!"
	print("PC2")
end)

end

Not getting any errors, and PC2 or MOBILE2 are not printing

sorry if my scripting is bad I am kind of a noob to scripting :sad:

1 Like

Why are you putting everything in while loops?

2 Likes

Yeah, probally should have used :PlayerAdded()

Still doesn’t explain why it doesn’t work

1 Like

Remove the loops from the local script, they’re completely useless. Also yeah, you should put it in PlayerAdded instead of loops in server script.

2 Likes

Yeah, ill do that when I get back into studio, thanks

Hey! Your problem is that you are trying to access UserInputService on the server which is not possible.

Therefore just do this in the localscript:

local UserInputService = game:GetService("UserInputService")

if UserInputService.TouchEnabled and not UserInputService.MouseEnabled then
-- Mobile
end

if not UserInputService.TouchEnabled and UserInputService.MouseEnabled then
-- PC
end
2 Likes

Doesn’t matter if he uses a PlayerAdded function because the UserInputService only works on the client.

2 Likes

I’ll give that a shot, let me open studio :+1:

2 Likes

I put the local script in the TextLabel, and nothing happened, even using prints

local UserInputService = game:GetService("UserInputService")

if UserInputService.TouchEnabled and not UserInputService.MouseEnabled then
	script.Parent.Text = "Mobile"
end

if not UserInputService.TouchEnabled and UserInputService.MouseEnabled then
	script.Parent.Text = "PC"
	
end
1 Like

I tested it myself and it works for me. Have you put it in a LocalScript?

1 Like

image
This is what I have set up, Should I try putting it in StarterPlayer/Character and using remote events?

1 Like

local scripts do not work in workspace. Instead, put a localscript in starterGui and do this:

local UserInputService = game:GetService("UserInputService")

if UserInputService.TouchEnabled and not UserInputService.MouseEnabled then
	game.Workspace.SmallGift.BillboardGui.TextLabel.Text = 'mobile'
end

if not UserInputService.TouchEnabled and UserInputService.MouseEnabled then
	game.Workspace.SmallGift.BillboardGui.TextLabel.Text = "PC"
end
2 Likes

Like @EncryptionFailure said, LocalScripts don’t run in workspace.

Therefore you can parent your BillboardGui to StarterGui:

Then you need to set the BillboardGui’s Adornee to the Part you originally wanted to parent the BillboardGui to:

1 Like


Thanks @EncryptionFailure and @EntryRoot for the help!

1 Like