Script not working on mobile

I’m trying to edit a script to make it work on mobile but its not working. I’ve gone over the script several times and searched for a fix but nothing works . the script works fine on pc and does not use key input and it even works on the mobile simulation in Roblox studio but when the script runs on a real mobile device it glitches out and does nothing except make the character go invisible for a few seconds which is not suppose to happen because its a fly script. Idk how to format this correctly but here is the script if anyone can tell me why its glitches out on mobile

LOCAL SCRIPT: wait(5)

sp=script
plr=game.Players.LocalPlayer

local speed=15
local topspeed=95

local rate=1/30

local inertia=1-(speed/topspeed)


local controls={forward=0,backward=0,left=0,right=0}
local momentum=Vector3.new(0,0,0)
local lastmomentum=Vector3.new(0,0,0)
local totalmomentum=0
local tilt=0
local lasttilt=0

local anim=script:WaitForChild("IdleANIM_Fly")
local anim=script:WaitForChild("FowardANIM_Fly")
local anim=script:WaitForChild("LeftANIM_Fly")
local anim=script:WaitForChild("Right2ANIM_Fly")

print"local"

function fly()		--(de)activate fly mode
	
	print"fly start"
	
if plr and plr.Character then
	print"past character"
			local torso=plr.Character:FindFirstChild("UpperTorso")
			local humanoid=plr.Character:FindFirstChild("Humanoid")
			if torso and humanoid  then
				momentum=torso.Velocity+(torso.CFrame.lookVector*10)+Vector3.new(0,0,0)

		local gyro=Instance.new("BodyGyro")
		
				gyro.Name="FlightGyro"
				gyro.P= 9000                  --10^6
				gyro.maxTorque=Vector3.new(gyro.P,gyro.P,gyro.P)
				gyro.cframe=torso.CFrame
				gyro.Parent=torso
		print"past gyro"
				velocity=Instance.new("BodyVelocity")
				velocity.Name="FlightVelocity"
				velocity.velocity=Vector3.new(0,0,0)
				velocity.P=10^4
				velocity.maxForce=Vector3.new(1,1,1)*(10^6)
				velocity.Parent=torso
		print"past velocity"
				
				

		while torso and humanoid and humanoid.Health>0 do
			print"while true do start"
					local movement=game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(Vector3.new(controls.left+controls.right,math.abs(controls.forward)*.2,controls.forward+controls.backward))*speed

			momentum=(momentum*inertia)+movement
			print"while true do mid"
					totalmomentum=momentum.magnitude
					if totalmomentum>topspeed then
						totalmomentum=topspeed
					end

					local tilt=((momentum*Vector3.new(1,0,1)).unit:Cross(((lastmomentum*Vector3.new(1,0,1)).unit))).y
			local tstilt=tostring(tilt)
			print"tilt"
					if tstilt=="-1.#IND" or tstilt=="1.#IND" or tilt==math.huge or tilt==-math.huge or tstilt==tostring(0/0) then
						tilt=0
					end

			local abstilt=math.abs(tilt)
			
			if 
				
				abstilt>.06 or abstilt<.0001 then
				print"tilt2"
						if math.abs(lasttilt)>.0001 then
							tilt=lasttilt*.9
						else
							tilt=0
						end
					else
						tilt=(lasttilt*.77)+(tilt*.25)			--weighted average
					end
					lasttilt=tilt

					if totalmomentum<.5 then
						momentum=Vector3.new(0,0,0)
						totalmomentum=0
						gyro.cframe=game.Workspace.CurrentCamera.CoordinateFrame
					else
						gyro.cframe=CFrame.new(Vector3.new(0,0,0),momentum)*CFrame.Angles(0,0,tilt*(-20))*CFrame.Angles(math.pi*(-.5)*(totalmomentum/topspeed),0,0)
					end
					velocity.velocity=momentum
					lastmomentum=momentum
					wait(rate)
				end
				
			end
		end
	end











delay(0,function()
print"fly delay"
fly()
	
end)







game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton.MouseButton1Down:Connect(function()
	controls.forward=-1


end)
game.Players.LocalPlayer.PlayerGui.ScreenGui.TextButton.MouseButton1Up:Connect(function()
	controls.forward=0


end)
2 Likes