Flying Mobile Support

I found this script somewhere and used it for a key part in my game, but it does not work on mobile (not good).

  1. What do you want to achieve? Make the inputs compatible with PC and mobile, basically, when the player goes left, the script detects and makes it go left, etc.

  2. What is the issue? every solution i think of, that should work, just makes the character only go foward.

  3. What solutions have you tried so far? UserInputSystem (to this day i don’t know how to use this), and ChatGPT (he did not know what was happening).

Code:

		mouse.Button1Up:connect(function()
			down=false
		end)
		mouse.KeyDown:connect(function(key2)
			local key=string.byte(key2)
			if key==32 then					--Space bar
				fly()
				local torso=sp.Parent:FindFirstChild("Torso")
				if torso~=nil then
					torso.Velocity=momentum
				end
			elseif key==string.byte("w") or key==17 then
				controls.forward=-1
			elseif key==string.byte("a") or key==20 then
				controls.left=-1
			elseif key==string.byte("s") or key==18 then
				controls.backward=1
			elseif key==string.byte("d") or key==19 then
				controls.right=1
			end
		end)
		mouse.KeyUp:connect(function(key2)
			local key=string.byte(key2)
			if key==string.byte("w") or key==17 then
				controls.forward=0
			elseif key==string.byte("a") or key==20 then
				controls.left=0
			elseif key==string.byte("s") or key==18 then
				controls.backward=0
			elseif key==string.byte("d") or key==19 then
				controls.right=0
			end
		end)
	end
end

If needed, i will see if i can find an example of the script just not doing its job.

Full script
--Made by Stickmasterluke
--What is Dragon Ball Z?


sp=script.Parent
plr=game.Players.localPlayer

--Bomb--
damage={0,0}
firerate=.5
bombvelocity=61.8
chargetime=0
extrasize=0
--------

--Flying--
speed=5
topspeed=50
----------

rate=1/30
debris=game:GetService("Debris")
equipped=false
check=true


inertia=1-(speed/topspeed)
flying=false
controls={forward=0,backward=0,left=0,right=0}
momentum=Vector3.new(0,0,0)
lastmomentum=Vector3.new(0,0,0)
totalmomentum=0
lastflap=0
tilt=0
lasttilt=0


function waitfor(a,b)
	while a:FindFirstChild(b)==nil do
		a.ChildAdded:wait()
	end
	return a:FindFirstChild(b)
end


local anim=waitfor(sp,"FlyAnimation")
local anim2=waitfor(sp,"ChargeEnergyBall")


function RemoveFlyStuff()
	local plr=game.Players.LocalPlayer
	if plr then
		local chr=plr.Character
		if chr then
			local torso=chr:FindFirstChild("Torso")
			if torso~=nil then
				for i,v in ipairs(torso:GetChildren()) do
					if v and (v.Name=="FlightGyro" or v.Name=="FlightVelocity") then
						v:remove()
					end
				end
			end
		end
	end
end

function fly()		--(de)activate fly mode
	if not equipped then
		flying=false
	else
		flying=not flying
	end
	RemoveFlyStuff()
	if flying then
		local torso=sp.Parent:FindFirstChild("Torso")
		local humanoid=sp.Parent:FindFirstChild("Humanoid")
		if torso and humanoid and humanoid.Health>0 then
			momentum=torso.Velocity+(torso.CFrame.lookVector*3)+Vector3.new(0,10,0)

			local gyro=Instance.new("BodyGyro")
			gyro.Name="FlightGyro"
			gyro.P=10^6
			gyro.maxTorque=Vector3.new(gyro.P,gyro.P,gyro.P)
			gyro.cframe=torso.CFrame
			gyro.Parent=torso

			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

			if flyanim~=nil then
				flyanim:Stop()
			end
			flyanim=humanoid:LoadAnimation(anim)
			if flyanim then
				flyanim:Play()
			end

			while flying and torso and humanoid and humanoid.Health>0 and equipped do
				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
				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
				-- Need to exhaustively check that tilt hasn't gotten into some weird value, these checks aren't exhaustive
				if tostring(tilt)=="-1.#IND" or tostring(tilt)=="1.#IND" or tilt==math.huge or tilt==-math.huge or tostring(0/0) == tostring(tilt) then
					tilt=0
				end
				local abstilt=math.abs(tilt)
				if abstilt>.06 or abstilt<.0001 then
					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
					--[[print("Momentum:" , momentum , "tilt:" , tilt , "totalmomentum:" , totalmomentum , "topspeed:" , topspeed)
					print(CFrame.new(Vector3.new(0,0,0),momentum) *
							CFrame.Angles(0,0,tilt*(-20)) *
							CFrame.Angles(math.pi*(-.5) * (totalmomentum/topspeed), 0, 0))
					--]]
					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
			RemoveFlyStuff()
			if flyanim~=nil then
				flyanim:Stop()
			end
			flying=false
		end
	end
end

function onEquipped(mouse)
	sp.Handle.Transparency=1
	equipped=true
	if chargeanim~=nil then
		chargeanim:Stop()
	end
	if mouse~=nil then
		mouse.Icon="rbxasset://textures\\GunCursor.png"
		mouse.Button1Down:connect(function()
			down=true
			local he=sp.Parent:FindFirstChild("Head")
			local hu=sp.Parent:FindFirstChild("Humanoid")
			if check and he and hu and hu.Health>0 then
				check=false
				mouse.Icon="rbxasset://textures\\GunWaitCursor.png"
				starttime=tick()
				lv=he.CFrame.lookVector
				local mesh=sp.Handle:FindFirstChild("Mesh")
				if mesh~=nil then
					mesh.Scale=Vector3.new(1,1,1)
				end
				local fire=sp.Handle:FindFirstChild("Fire")
				if fire~=nil then
					fire.Size=0
					fire.Enabled=true
				end
				if chargeanim~=nil then
					chargeanim:Stop()
				end
				chargeanim=hu:LoadAnimation(anim2)
				if chargeanim then
					chargeanim:Play()
				end
				local lastchargesound=0
				while down and equipped and tick()-starttime<chargetime and hu~=nil and hu.Health>0 do
					local percent=(tick()-starttime)/chargetime
					sp.Handle.Transparency=1+(math.random()*.3)
					sp.GripPos=Vector3.new(0,0,.5+(extrasize*.5*percent))
					local mesh=sp.Handle:FindFirstChild("Mesh")
					if mesh~=nil then
						mesh.Scale=Vector3.new(1,1,1)+(Vector3.new(1,1,1)*extrasize*percent)
					end
					local fire=sp.Handle:FindFirstChild("Fire")
					if fire~=nil then
						fire.Size=percent*extrasize*2
					end
					if tick()-lastchargesound>5 then
						lastchargesound=tick()
						local sound=sp.Handle:FindFirstChild("ChargeSound")
						if sound~=nil then
							sound:Play()
						end
					end
					wait()
				end
				sp.GripPos=Vector3.new(0,0,0)
				sp.Handle.Transparency=1
				local sound=sp.Handle:FindFirstChild("ChargeSound")
				if sound~=nil then
					sound:Stop()
				end
				local sound=sp.Handle:FindFirstChild("FireSound")
				if sound~=nil then
					sound:Play()
				end
				local fire=sp.Handle:FindFirstChild("Fire")
				if fire~=nil then
					fire.Enabled=false
				end
				local shot=sp.Handle:clone()
				if chargeanim~=nil then
					chargeanim:Stop()
				end
				local fire=shot:FindFirstChild("Fire")
				if fire~=nil then
					fire.Enabled=true
				end
				local percent=math.min(math.max((tick()-starttime)/chargetime,0),1)
				local size=(1+(extrasize*percent))
				shot.Size=Vector3.new(1,1,1)*size
				shot.Mesh.Scale=Vector3.new(1,1,1)*(size/shot.Size.Y)
				shot.Name="Effect"
				shot.CanCollide=false
				shot.Transparency=1
				if mouse~=nil then
					shot.Velocity=((mouse.Hit.p-shot.Position).unit)*bombvelocity
				else
					shot.Velocity=lv*bombvelocity
				end
				shot.RotVelocity=Vector3.new(math.random()-.5,math.random()-.5,math.random()-.5)*20
				local bf=Instance.new("BodyForce")
				bf.force=Vector3.new(0,shot:GetMass()*196.2,0)
				bf.Parent=shot
				shot.Touched:connect(function(hit)
					if shot~=nil and shot.Transparency~=1 and hit~=nil and string.lower(string.sub(hit.Name,1,6))~="effect" and hit.Name~="Handle" and hit.Name~="Water" then
						if not hit:IsDescendantOf(sp.Parent) then
							local hum=hit.Parent:FindFirstChild("Humanoid")
							if hum~=nil then
								for _,v in pairs(hum:GetChildren()) do
									if v~=nil and v.Name=="creator" then
										v:remove()
									end
								end
								local ct=Instance.new("ObjectValue")
								ct.Name="creator"
								ct.Value=game.Players.LocalPlayer
								ct.Parent=hum
								hum:TakeDamage(damage[1]+(percent*(damage[2]-damage[1])))
								local sound=shot:FindFirstChild("ExplodeSound")
								if sound~=nil then
									sound.Volume=.5+(.5*percent)
									sound:Play()
								end
								shot.Transparency=1
								shot.Anchored=true
								local fire=shot:FindFirstChild("Fire")
								if fire~=nil then
									fire.Enabled=false
								end
								wait(3)
								if shot~=nil then
									shot:remove()
								end
							end
						end
					end
				end)
				debris:AddItem(shot,14)
				shot.Parent=game.Workspace
				wait(firerate)
				mouse.Icon="rbxasset://textures\\GunCursor.png"
				check=true
			end
		end)
		mouse.Button1Up:connect(function()
			down=false
		end)
		mouse.KeyDown:connect(function(key2)
			local key=string.byte(key2)
			if key==32 then					--Space bar
				fly()
				local torso=sp.Parent:FindFirstChild("Torso")
				if torso~=nil then
					torso.Velocity=momentum
				end
			elseif key==string.byte("w") or key==17 then
				controls.forward=-1
			elseif key==string.byte("a") or key==20 then
				controls.left=-1
			elseif key==string.byte("s") or key==18 then
				controls.backward=1
			elseif key==string.byte("d") or key==19 then
				controls.right=1
			end
		end)
		mouse.KeyUp:connect(function(key2)
			local key=string.byte(key2)
			if key==string.byte("w") or key==17 then
				controls.forward=0
			elseif key==string.byte("a") or key==20 then
				controls.left=0
			elseif key==string.byte("s") or key==18 then
				controls.backward=0
			elseif key==string.byte("d") or key==19 then
				controls.right=0
			end
		end)
	end
end

function onUnequipped()
	equipped=false
end

sp.Equipped:connect(onEquipped)
sp.Unequipped:connect(onUnequipped)