local function updatePlaneVelocity()
bodyVelocity.MaxForce = Vector3.new(11250, 11250, 11250) * ( planeProperties.Current_Speed / 1)
bodyVelocity.Velocity = mouse.Hit.LookVector * (planeProperties.Current_Speed / 1.2)
end
local function updatePlaneGyro()
gyro.CFrame = CFrame.new(plane.PrimaryPart.Position, mouse.Hit.LookVector * gyro.P)
end
local function updatePlaneSpeed(bool)
--[[
Handles the speed on the boolean. If true, increases it and
if false or nil, decreases it. Sanity checks kicks in as well.
]]
if bool then
if planeProperties.Current_Speed < planeProperties.Max_Speed then
planeProperties.Current_Speed += 1
end
else
if planeProperties.Current_Speed > planeProperties.Stall_Speed then
planeProperties.Current_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
]]
bodyVelocity.MaxForce = Vector3.new()
planeProperties.Current_Speed = planeProperties.Stall_Speed
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 and not gameProcessed then
while UserInputService:IsKeyDown(Enum.KeyCode.W) and wait() do
updatePlaneSpeed(true)
end
while 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
resetPlanePropertyValues() -- Calls the function to reset all the values associated with the plane
--]]
humanoid.Died:Connect(function()
resetPlanePropertyValues()
wait()
script:Destroy()
end)
if not seatValue.Value then -- No Player found in seat, turn off the engine
planeEngineOn = nil
wait()
script:Destroy()
end
if planeEngineOn then
updatePlaneGyro()
updatePlaneVelocity()
updateEngineColor()
else
resetPlanePropertyValues()
end
end)
end)
Here is my ‘half’ code used to handle my plane. It functions properly as intended.
I am not satisfied with how I handle the plane despite the fact it works properly. I need advice on how can I make it more efficient and more readable.
Here’s what I want reviewed.
- Readability of code
- Variables
- Efficiency
If you think my code is efficient and don’t seem to review it, do let me know.