local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local mouse = player:GetMouse()
local remotesFolder = ReplicatedStorage.Remotes.Plane
local serverReplicationFolder = remotesFolder.ServerReplication
local planesFolder = workspace.Planes
-- Plane Variables
local planeEngineOn = nil
local MAX_PLANE_SPEED = 175
local CURRENT_PLANE_SPEED = 0
local STALL_PLANE_SPEED = 0
-- Events
remotesFolder.Plane_Control.OnClientEvent:Connect(function(plane)
-- Folders
local bodyVelocity = plane.PrimaryPart.BodyVelocity
local gyro = plane.PrimaryPart.BodyGyro
local afterBurner = plane.Engine.Afterburner
local gunHolder = plane.GunHolder
local gun = plane.Gun
local seatValue = plane.Handler.Driver
if not plane.PrimaryPart then
warn(string.format("No primary part found in %s %s", plane.Name, ", expected 'PrimaryPart' "))
return
end
local function updateEngineColor()
remotesFolder.ServerReplication.ReplicateEngineColor:FireServer(afterBurner, CURRENT_PLANE_SPEED)
end
local function updatePlaneEngineSound()
-- Unfinished
end
local function updatePlaneVelocity()
bodyVelocity.MaxForce = Vector3.new(19250, 19250, 19250) * ( CURRENT_PLANE_SPEED / 1.2)
bodyVelocity.Velocity = mouse.Hit.LookVector * CURRENT_PLANE_SPEED
end
local function updatePlaneGyro()
gyro.CFrame = CFrame.new(plane.PrimaryPart.Position, mouse.Hit.LookVector * gyro.P)
end
local function updatePlaneSpeed(bool)
if bool then
if CURRENT_PLANE_SPEED < MAX_PLANE_SPEED then
CURRENT_PLANE_SPEED += 1
end
else
if CURRENT_PLANE_SPEED > STALL_PLANE_SPEED then
CURRENT_PLANE_SPEED -= 1
end
end
end
local function resetPlanePropertyValues()
--[[ Reset plane property values
Set's the plane speed, it's max force of body velocity and engine back
to default. If statements to prevent value changing unnecessarily
// MaxForce = Vector3.new()
// MaxTorque = Vector3.new()
// Plane_Speed = Default or 0
]]
if bodyVelocity.MaxForce ~= Vector3.new() then
bodyVelocity.MaxForce = Vector3.new()
end
if CURRENT_PLANE_SPEED > STALL_PLANE_SPEED then
CURRENT_PLANE_SPEED = STALL_PLANE_SPEED
end
updatePlaneVelocity()
updateEngineColor()
end
--[[ Input Handler
Uses a function to handle plane speed
]]
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed and input.KeyCode == Enum.KeyCode.E then
planeEngineOn = not planeEngineOn
end
if planeEngineOn then
while not gameProcessed and UserInputService:IsKeyDown(Enum.KeyCode.W) and wait() do
updatePlaneSpeed(true)
end
while not gameProcessed and UserInputService:IsKeyDown(Enum.KeyCode.S) and wait() do
updatePlaneSpeed(nil)
end
end
end)
RunService.Stepped:Connect(function()
--[[
-- Fires on physics simulation, useful when handling objects
unanchored and their properties associated with it.
If statements used to prevent values changing unnecessarily, helps in keeping
efficient memory, though only improving 0.000000000000001% memory.
-----------------------------------------------------------------------------
/ Functions
default() -- Calls the function to reset all the values associated with the plane
--]]
if not planesFolder:FindFirstChildWhichIsA("Model") then
wait()
script:Destroy()
end
if not seatValue.Value then -- No Player found in seat, turn off the engine
if not planeEngineOn then
planeEngineOn = nil
end
wait()
script:Destroy()
end
if planeEngineOn then
updatePlaneGyro()
updatePlaneVelocity()
updateEngineColor()
else
resetPlanePropertyValues()
return
end
end)
end)
Updated: Almost the whole script.
Here’s what I want reviewed:
- Readability of code
- Efficiency of code