--		local throttleNeeded = 


		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
			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
		updateGui(tax,stl)	-- Keep the pilot informed!
		main.Throttle.Value = throttle 
		if currentSpeed > 0 then
			game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Attach
		else
			game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Follow
		end 
		wait()
	end
end

script.Parent.ImageButton.MouseEnter:connect(function(m)	-- Initial setup
	script.Parent.ImageButton:Destroy()
	mouse = game.Players.LocalPlayer:GetMouse()
	mouseSave = mouse
	mouse.Icon = "http://www.roblox.com/asset/?id=48801855"	-- Mouse icon used in Perilous Skies
	panel.Visible=true
	delay(0,function() fly(mouse) end)	-- Basically a coroutine
	mouse.KeyDown:connect(function(k)
		if (not flying) then return end
		k = k:lower()
		if (k == keys.engine.key) then
			main.Engine.Value = true
			script.Parent.Panel.Engines.BackgroundColor3 = Color3.new(255/255, 255/255, 0)
			script.Parent.Panel.Engines.BorderColor3 = Color3.new(170/255, 170/255, 0)
			wait (20)
			script.Parent.Panel.Engines.BackgroundColor3 = Color3.new(85/255, 255/255, 0)
			script.Parent.Panel.Engines.BorderColor3 = Color3.new(0, 85/255, 0)
			on = (not on)
			if (not on) then throttle = 0 
			main.Engine.Value = false
			script.Parent.Panel.Engines.BackgroundColor3 = Color3.new(255/255, 255/255, 0)
			script.Parent.Panel.Engines.BorderColor3 = Color3.new(170/255, 170/255, 0)
			wait (20)
			script.Parent.Panel.Engines.BackgroundColor3 = Color3.new(196/255, 40/255, 28/255)
			script.Parent.Panel.Engines.BorderColor3 = Color3.new(100/255, 0, 0)
			end
		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)
	mouse.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 exitplane()
		flying = false
		game.Workspace.CurrentCamera.CameraType = Enum.CameraType.Custom
		mouseSave.Icon = "rbxasset://textures\\ArrowCursor.png"
		script.Parent:Destroy()
		player.Character.Freeze.Captian.Value = false
		player.Character.Humanoid.JumpPower = 50
		player.Character.Humanoid.Jump = true
end

panel.ExitButton.MouseButton1Down:connect(exitplane)