Click to fly plane

Serverscript (inside vehicleseat):

local seat = script.Parent
local hum = script.Parent.Parent:WaitForChild("Humanoid")

local ls = nil

local body = seat.Parent.Hull
for i, v in pairs(seat.Parent:GetChildren()) do
	if v:IsA("BasePart") and v ~= body then
		local weld = Instance.new("WeldConstraint", body)
		weld.Part0 = body
		weld.Part1 = v
	end
end

function setNetOwner(owner)
	for i, v in pairs(seat.Parent:GetDescendants()) do
		if v:IsA("BasePart") then
			v:SetNetworkOwner(owner)
		end
	end
end

function newPilot()
	if body:FindFirstChild("shipWeld") then
		body.shipWeld:Destroy()
	end
	if seat.Occupant == nil then
		script.Parent.engine:Stop()
		delay(20, function()
			if seat.Occupant == nil then
				seat.Parent:Destroy()
			end
		end)
		if ls then
			ls:Destroy()
		end
		setNetOwner(nil)
	else
		seat.Parent.Parent = game.Workspace
		local player = game.Players:FindFirstChild(seat.Occupant.Parent.Name)
		script.Parent.engine:Play()
		local char = seat.Occupant.Parent
		local plr = game.Players:GetPlayerFromCharacter(char)
		ls = script.LocalScript:Clone()
		ls.Parent = char
		setNetOwner(plr)
	end
end

function hc()
	if hum.Health <= 0 then
		local part = Instance.new("Part",workspace:FindFirstChild("Sounds"))
		part.Anchored = true
		part.Size = Vector3.new(0,0,0)
		part.Position = script.Parent.Position
		part.Transparency = 1
		part.CanCollide = false
		local sound = Instance.new("Sound",part)
		sound.SoundId = "rbxassetid://6290067239"
		sound.Volume = 2
		sound:Play()
		local explode = Instance.new("Explosion",workspace)
		explode.Position = seat.Parent.Hull.Position
		script.Parent.Parent.Parent = workspace.Debris
		hum.Parent:Destroy()
	end
end

seat:GetPropertyChangedSignal("Occupant"):Connect(newPilot)
hum.HealthChanged:Connect(hc)

Localscript (inside serverscript):

local uis = game:GetService("UserInputService")
local cas = game:GetService("ContextActionService")
local char = script.Parent
local human = char:WaitForChild("Humanoid")
local body = script:WaitForChild("body").Value
local rs = game:GetService("RunService")
local reps = game:GetService("ReplicatedStorage")

local bv = Instance.new("BodyVelocity", body)
local gyro = Instance.new("BodyGyro", body)

local max = 100000000

bv.MaxForce = Vector3.new(max, max, max)
gyro.MaxTorque = Vector3.new(max, max, max)

local flying = true

function eject()
	cas:UnbindAction("Fly")
	bv:Destroy()
	gyro:Destroy()
end

local function toggle(name,state,object)
	if name == "Fly" and state == Enum.UserInputState.Begin then
		if flying then
			flying = false
			bv.MaxForce = Vector3.new(0, 0, 0)
			gyro.MaxTorque = Vector3.new(0, 0, 0)
		else
			flying = true
			bv.MaxForce = Vector3.new(max, max, max)
			gyro.MaxTorque = Vector3.new(max, max, max)
		end
	end
end

rs.RenderStepped:Connect(function(step)
	local cam = workspace.CurrentCamera
	bv.Velocity = body.CFrame.RightVector * 75
	gyro.CFrame = cam.CFrame * CFrame.Angles(0, math.rad(90), 0)
	local aav = body.CFrame:VectorToObjectSpace(body.AssemblyAngularVelocity)
	gyro.CFrame *= CFrame.Angles(-math.rad(aav.Y)*20, 0, 0)
end)

cas:BindAction("Fly",toggle,true,Enum.KeyCode.E,Enum.KeyCode.ButtonY)
cas:SetTitle("Fly","E")
cas:SetPosition("Fly",UDim2.new(1, -70, 0, 10))
human.Died:Connect(eject)
body.Parent.VehicleSeat:GetPropertyChangedSignal("Occupant"):Connect(eject)

These scripts allow you to fly a plane so that when you move your mouse with shift lock it flies towards where youre facing. However, I’d like to make it so it only moves when you click somewhere, exactly like in this game: Naval Warfare - Roblox

Thanks!

1 Like