Need Help Trying to Get Plane System Working Smoothly

I’m currently trying to rework a plane script a developer of mine made for me over a year ago, as mentioned here.

Anyway, whenever I try to have this code run, which is fired via sending the localscript into the player and detecting mouse movements, it crashes the testing place.

local seat = script:WaitForChild("this")
local connection = seat.Value
local USINS=game:getService("UserInputService")
local player=game.Players.LocalPlayer
local rem=script.Parent.Motor.remotes
local ControlModule = require(game:GetService("Players").LocalPlayer.PlayerScripts.PlayerModule.ControlModule)
local Speed = 100
local Turn = 0
local ClickMove = false --If enabled, your jet will only move towards the last clicked location.
local Mouse = player:GetMouse()
local fly = connection.Parent:WaitForChild("fly")
local gyro = connection.Parent.Engine.BodyGyro
local velo = connection.Parent.Engine.BodyVelocity
if fly ~= nil then
	print("success")
end
if USINS.TouchEnabled then
	ClickMove = true
end
local Down = false

function GetBankAngle(M) --This function calculates the Bank Angle
	local VSX,X = M.ViewSizeX,M.X
	local Ratio = (((VSX/2) - X)/(VSX/2))
	Ratio = (Ratio < -1 and -1 or Ratio > 1 and 1 or Ratio)
	return math.rad(Ratio * 80)
end

if connection.Parent ~= nil then
	while fly.Value == true do
		if velo.MaxForce == Vector3.new(math.huge,math.huge,math.huge) and not USINS:GetFocusedTextBox() then
			Mouse.TargetFilter = game.Workspace
			local BankAngle = GetBankAngle(Mouse)
			gyro.maxTorque = Vector3.new(math.huge,math.huge,math.huge)
			velo.maxForce = Vector3.new(math.huge,math.huge,math.huge)
			gyro.cframe = (Mouse.Hit*CFrame.Angles(0,0,BankAngle))
			velo.velocity = (connection.Parent.Engine.CFrame.lookVector)*100
		end	
	end
end

I’m still new to using body movers and the like, so I don’t understand what the issue is (probably an infinite loop deal, but I have no idea what the base case should be). Instead of a while loop, I would use a function that would determine when the player moved their mouse; it did not crash studio, but it would get wonky flight paths like this:
https://gyazo.com/0e112ef728eefcb6ca09f728c842639b
As you can see, the alignment of the jet and its velocity were not consistent.
Here is the following code for this system:

----client sided control script
---insert values from above code here

Mouse.TargetFilter = game.Workspace
Mouse.Move:connect(function() 
	--while fly.Value == true do
	Mouse.TargetFilter = game.Workspace
	connection.controls:FireServer("move",Mouse.Hit,Mouse.Hit.p,Mouse.ViewSizeX,Mouse.X)
end)

--- server-sided control script
local engine = script.Parent.Parent:WaitForChild("Engine")
local gyro = engine:WaitForChild("BodyGyro")
local velo = engine:WaitForChild("BodyVelocity")
local m

function GetBankAngle(viewx, x) --This function calculates the Bank Angle
	local VSX,X = viewx,x
	local Ratio = (((VSX/2) - X)/(VSX/2))
	Ratio = (Ratio < -1 and -1 or Ratio > 1 and 1 or Ratio)
	return math.rad(Ratio * 80)
end

script.Parent.controls.OnServerEvent:Connect(function(plr, command, mouse, mPos, xview, x)
	if command == "move" then
		if mouse ~= nil then
			m = mouse
			local BankAngle = GetBankAngle(xview, x)
			gyro.maxTorque = Vector3.new(math.huge,math.huge,math.huge)
			velo.maxForce = Vector3.new(math.huge,math.huge,math.huge)
			gyro.cframe = (mouse*CFrame.Angles(0,0,BankAngle))
			velo.velocity = (engine.CFrame.lookVector)*100
		end
	end
end)
2 Likes

As an FYI, “fly.Value” is triggered by a server script. A player leaving/entering the plane flicks it true/false.

Does anyone know why this crashes the game, and why the movement on the other option is very inconsistent