Script won't work on mobile, but will work on roblox Mobile emulator

Hello, again! So I’ve been trying to make my game more mobile-friendly for its release, but it won’t work for mobile players but it will work on the Roblox Mobile emulator, even if I use my Pc’s touch screen, but I’m not sure why it won’t work on actual mobile devices. In this script, I use the UserInputService, TouchTap, but it won’t work with any touch events anyway. I did a simple test by creating a script that creates a part every time the player touches the screen and it works, so it must be a problem with my code. But even then, when I use the Roblox emulator this script works as it should, but it is not receiving inputs from mobile devices. Below I have left my script, it is a local script. And I have no errors when the event is fired and it all works perfectly, but not on mobile devices but it does work on the Roblox mobile emulator and touch screen of my laptop.

local players = game:GetService("Players")
local player = players.LocalPlayer
local Character = player.Character or player.CharacterAdded:Wait()
local Mouse = player:GetMouse()
local PlayerStats = Character:FindFirstChild("PlayerStats")
local Magic = PlayerStats:WaitForChild("Magic")
local CurrentMagic = Magic.CurrentMagic 
local MaxMagic = Magic.MaxMagic
local FireCircleEvent = game.ReplicatedStorage.SpellEvents:WaitForChild("FireCircleSpell") 
local UIS = game:GetService("UserInputService")
local connect
local Debounce = true 



player.Chatted:Connect(function(msg)
	if string.lower(msg) == "phasmatos incendia circulum" and Debounce == true then 
		Debounce = false 
		
		connect = UIS.TouchTap:Connect(function(tp, GPE)
			if Mouse.Target.Parent:FindFirstChild("Humanoid") and CurrentMagic.Value >= 80 then 
				local Victim = Mouse.Target.Parent
				local Distance = (Character.HumanoidRootPart.Position - Victim.HumanoidRootPart.Position)
				
				if Distance <= 25 then 
					
					Debounce = true 
					FireCircleEvent:FireServer(Debounce, Victim, msg)
					Debounce = false 
					connect:Disconnect()
					wait(5)
					Debounce = true
					-- (player, Debounce, victim, msg)
				end
			end
		end)
	end
end)

Thank you for anyone who helps me! I really appreciate it!!

A common issue with mobile users is overlayed frames in the way of a button. If a button is being overlayed by a textbutton or any gui element, be sure it’s Active property is set to false.

2 Likes

Hello! Thank you for your advice it helped with some other issues but none of my scripts still work or get activated when the event should fire. Do you have any other solutions or notions as to why this problem still occurs?

Firstly, since you are trying to find when the player touches exclusively the game world, and not gui, you should be using UIS.TouchTapInWorld to find when the player touches a character in the game world. UserInputService | Documentation - Roblox Creator Hub

Next, the main issue I find is that you are indexing the mouse when a player taps their screen. This is a buggy approach since the mouse can disappear when the fingers are released, yielding a nil reference. Using the parameters of the UIS.TouchTap or UIS.TouchTapInWorld events gives you the state of the mouse exactly when the event was fired:

  1. Vector2 - position of the tap
  2. bool - game/UI processed event

Pass the Vector2 to game.Workspace.CurrenctCamera:ViewportPointToRay() and you can raycast from the screen to the 3D world, returning the part that is hit.
Example below:

local camera = game.Workspace.CurrentCamera

--// code

		connect = UIS.TouchTapInWorld:Connect(function(tp, GPE)
			if GPE then return end

			local unitRay = camera:ViewportPointToRay(tp.X, tp.Y)
			local ray = Ray.new(unitRay.Origin, unitRay.Direction * 25)
			local hit, worldPosition = game.Workspace:FindPartOnRay(ray)
			
			if hitPart.Parent:FindFirstChild("Humanoid") and CurrentMagic.Value >= 80 then 
				--// code
			end
		end)