Why does this ball work so weirdly on Mobile?

See I have a ball that flings when you point and click anywhere in that direction and this is the code for it:

evvB.OnServerEvent:Connect(function(player, p)
	if table.find(deb, player) then return end 
	table.insert(deb, player)
	local g = Vector3.new(0, -game.Workspace.Gravity, 0)
	local x0 = ball.CFrame * Vector3.new(0, 0, 0)
	local v0 = (p - x0 - 0.25*g*t*t)/t
	local c = ball:Clone()
	c.Name = "BlueBallC"
	c.Velocity = v0
	--c.CFrame = CFrame.new(x0)
	c.CanCollide = true
	c.Anchored = false
	c.Parent = workspace
	task.wait(9)
	local index = table.find(deb, player)
	if index then 
		table.remove(deb, index)
	end
end)

See now the thing is it works perfectly on PC like this because the ‘p’ is the position of the mouse click, which is fire to the server and the the actual motion happens on the server.
robloxapp-20211229-1156190.wmv (825.7 KB)

But on mobile I wanted it to be a drag function like swipe throw the ball if you get what I mean and I did it like this:

local ts = game:GetService("Teams")
local Remote1 = game.ReplicatedStorage.MotionBluM 
local lp = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")

uis.TouchStarted:Connect(function(to, gpss)
uis.TouchMoved:Connect(function(touch, gp)
	uis.TouchEnded:Connect(function(touc, gps)
	if lp.Team == ts["Blue"] then
		print(touc.Position)
		Remote1:FireServer(touc.Position)
	    end
    end)
	end)
	end)

And the same code is being used on the server for it as well the ‘p’ in this case is the touch ended position however it is not working properly and does this… : Streamable Video

Please tell me what I can do to fix this and do a proper swipe/drag function on the ball for mobile players.

I don’t really ever develop for mobile, but I’ll give this a shot.

I have a feeling that this may be something to do with the fact that you don’t disconnect your functions after you’re finished with them. :Connect() returns an RBXScriptSignal object that provides things for connections like in your case.

If you do:

local Connection = uis.TouchStarted:Connect(function() bla bla bla end)

then you can call Connection:Disconnect() to disconnect that function and stop it from being fired.
In your case, when the user stops swiping the screen, disconnect the other TouchMoved and TouchEnded events. (Disconnecting events when you dont need them also improves performance, as they take do take some CPU time even when idle, and performance is very important on mobile platforms!)

Just a general note: Connecting several functions that do the same thing to the same event is a bad idea!

Also, when you bind TouchMoved to connect a TouchEnded event, that will connect the event every time a frame occurs that the finger is moving on the screen. Connect the TouchMoved event before the TouchEnded event (events are connected asynchronously, meaning they do not pause the script while they happen). That way, the TouchEnded event only gets connected once (and can get disconnected when you have to).

Apologies for such a wordy answer! If you need anything explained, just message me.

Edit: The script should look something like this:

local ts = game:GetService("Teams")
local Remote1 = game.ReplicatedStorage.MotionBluM 
local lp = game.Players.LocalPlayer
local uis = game:GetService("UserInputService")

local TouchStartedConn = uis.TouchStarted:Connect(function(to, gpss)
	local TouchMovedConn = uis.TouchMoved:Connect(function(touch, gp)
		if lp.Team == ts["Blue"] then
			print(touch.Position)
			Remote1:FireServer(touch.Position)
		end
	end)
	
	uis.TouchEnded:Connect(function(touc, gps)
		TouchMovedConn:Disconnect()
	end)
end)

Yeah but now it’s not even working I tried your script It doesn’t throw the ball anymore.

Well, like I mentioned, I don’t develop for mobile, sorry. Are there any errors at all? You can check on mobile by typing /console in the chat.