--		local throttleNeeded = 
		animationParts.Wings.WingFlex:FireServer()
		local stallLine = ((stallSpeed/math.floor(currentSpeed+0.5))*(stallSpeed/max))
		stallLine = (stallLine > 1 and 1 or stallLine)
		panel.Throttle.Bar.StallLine.Position = UDim2.new(stallLine,0,0,0)
		panel.Throttle.Bar.StallLine.BackgroundColor3 = (stallLine > panel.Throttle.Bar.Amount.Size.X.Scale and Color3.new(1,0,0) or Color3.new(0,0,0))
		if (change == 1) then
			currentSpeed = (currentSpeed > desiredSpeed and desiredSpeed or currentSpeed)	-- Reduce "glitchy" speed
		else
			currentSpeed = (currentSpeed < desiredSpeed and desiredSpeed or currentSpeed)
		end
		local tax,stl = taxi(),stall()
		if ((lastStall) and (not stl) and (not tax)) then	-- Recovering from a stall:
			if ((realSpeed > -10000) and (realSpeed < 10000)) then
				currentSpeed = realSpeed
			else
				currentSpeed = (stallSpeed+1)
			end
		end
		lastStall = stl
		move.velocity = (main.CFrame.lookVector*currentSpeed)		-- Set speed to aircraft
		local bank = ((((m.ViewSizeX/2)-m.X)/(m.ViewSizeX/2))*maxBank)	-- My special equation to calculate the banking of the plane. It's pretty simple actually
		bank = (bank < -maxBank and -maxBank or bank > maxBank and maxBank or bank)
		if (tax) then
			animationParts.Wings.WingStraight:FireServer()
			if (currentSpeed < 2) then	-- Stop plane from moving/turning when idled on ground
				move.maxForce = Vector3.new(0,0,0)
				gyro.maxTorque = Vector3.new(0,0,0)
			else
				move.maxForce = Vector3.new(math.huge,0,math.huge)	-- Taxi
				gyro.maxTorque = Vector3.new(0,math.huge,0)
				gyro.cframe = CFrame.new(main.Position,m.Hit.p)
			end
		elseif (stl) then
			move.maxForce = Vector3.new(0,0,0)	-- Stall
			gyro.maxTorque = Vector3.new(math.huge,math.huge,math.huge)
			gyro.cframe = (m.Hit*CFrame.Angles(0,0,math.rad(bank)))
		else
			move.maxForce = Vector3.new(math.huge,math.huge,math.huge)	-- Fly
			gyro.maxTorque = Vector3.new(math.huge,math.huge,math.huge)
			gyro.cframe = (m.Hit*CFrame.Angles(0,0,math.rad(bank)))
		end
		if ((altRestrict) and (main.Position.y < altMin)) then	-- If you have altitude restrictions and are below the minimun altitude, then make the plane explode
			plane.AutoCrash.Value = true
		end
		if main.Position.Y > 1000 then
			animationParts.Wings.WingFlex:FireServer()
		end
		local bank = ((((m.ViewSizeX/2)-m.X)/(m.ViewSizeX/2)))	-- My special equation to calculate the banking of the plane. It's pretty simple actually
		local pitch = (((m.ViewSizeY/2)-m.Y)/(m.ViewSizeY/2))
		local Control1 = 0.3*pitch
		local Control2 = 0.5*-bank
		local Control3 = 0.2*bank
		local Control4 = 1*bank
		local Control5 = 0.2*pitch
		local Control6 = 0.2*bank
		animationParts.ControlFE:FireServer(Control1,Control2,Control3,Control4,Control5,Control6)
		main.FE.ThrottleFE:FireServer(throttle)
		updateGui(tax,stl)	-- Keep the pilot informed!
		wait()
	end
end

function CamChange()
	if camtype == 1 then
		game.Workspace.CurrentCamera.CameraSubject = mainParts.Camera
		workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
	elseif camtype == 2 then
		game.Workspace.CurrentCamera.CameraSubject = mainParts.Camera
		workspace.CurrentCamera.CameraType = Enum.CameraType.Attach
	elseif camtype == 3 then
		game.Workspace.CurrentCamera.CameraSubject = player.Character.Humanoid
		workspace.CurrentCamera.CameraType = Enum.CameraType.Attach
	end
end

game:GetService("UserInputService").InputBegan:Connect(function(key, gameProcessedEvent)
	if not gameProcessedEvent then
		if key.KeyCode == Enum.KeyCode.Z then
			if camtype == 1 then
				camtype = 2
				CamChange()
			elseif camtype == 2 then
				camtype = 3
				CamChange()
			elseif camtype == 3 then
				camtype = 1
				CamChange()
			end
		end
	end
end)

function PilotIn()	-- Initial setup
	selected,script.Parent.CurrentSelect.Value = true,true
	m = player:GetMouse()
	mouseSave = m
	CamChange()
	m.Icon = "http://www.roblox.com/asset/?id=2763570264"	-- Mouse icon used in Perilous Skies
	delay(0,function() fly(m) end)	-- Basically a coroutine
	m.KeyDown:connect(function(k)
		if (not flying) then return end
		k = k:lower()
		if (k == keys.engine.key) then
			on = (not on)
			main.FE.OnFE:FireServer() -- FE
			if (not on) then 
			throttle = 0 
			main.FE.OffFE:FireServer() -- FE
			end
		elseif (k == keys.landing.key) then
			main.FE.GearFE:FireServer() -- FE
		elseif (k:byte() == keys.spdup.byte) then
			keys.spdup.down = true
			delay(0,function()
				while ((keys.spdup.down) and (on) and (flying)) do
					throttle = (throttle+throttleInc)
					throttle = (throttle > 1 and 1 or throttle)
					wait()
				end
			end)
		elseif (k:byte() == keys.spddwn.byte) then
			keys.spddwn.down = true
			delay(0,function()
				while ((keys.spddwn.down) and (on) and (flying)) do
					throttle = (throttle-throttleInc)
					throttle = (throttle < 0 and 0 or throttle)
					wait()
				end
			end)
		end
	end)
	m.KeyUp:connect(function(k)
		if (k:byte() == keys.spdup.byte) then
			keys.spdup.down = false
		elseif (k:byte() == keys.spddwn.byte) then
			keys.spddwn.down = false
		end
	end)
end

function deselected(forced)
	selected,script.Parent.CurrentSelect.Value = false,false
	game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
	game.Workspace.CurrentCamera.CameraSubject = player.Character.Humanoid
	gui.Parent = nil
	flying = false
	pcall(function()
		if on == false then
			move.maxForce = Vector3.new(0,0,0)
			if (taxi()) then
				gyro.maxTorque = Vector3.new(0,0,0)
			end
		end
	end)
	if (forced) then
		if (mouseSave) then
			mouseSave.Icon = ""	-- If you remove a tool without the actual deselect event, the icon will not go back to normal. This helps simulate it at the least
			wait()
		end
		script.Parent.Deselect1.Value = true	-- When this is triggered, the Handling script knows it is safe to remove the tool from the player
	end
end

player.Character.Humanoid.Jumping:Connect(deselected)

script.Parent.Deselect0.Changed:connect(function()	-- When you get out of the seat while the tool is selected, Deselect0 is triggered to True
	if (script.Parent.Deselect0.Value) then
		deselected(true)
	end
end)

PilotIn()