I made a Roblox car script that has automatic gear shifting (and it’s probably one of my most hard-to-script projects I ever made), I need tips on how to make my code easier to read because I’ve been struggling to get this working, and I’m still working on it.
driverSeat = script.Parent
chassis = script.Parent.Parent:FindFirstChild("Body")
gears = {
["1"] = 30,
["2"] = 60,
["3"] = 90,
["4"] = 120
}
currentGear = "1"
function shiftGears()
--check if player is accelerating and is at the gear's top speed
if (driverSeat.Throttle == 1 and driverSeat.Velocity.Magnitude >= gears[currentGear] - 10) then
--avoid shifting to a gear that doesn't exist
if tonumber(currentGear) + 1 <= 4 then
currentGear = tostring(tonumber(currentGear) + 1)
end
--check if player is braking and the current gear is above 1.
elseif (driverSeat.Throttle < 0 and tonumber(currentGear) > 1) and (driverSeat.Velocity.Magnitude <= gears[currentGear] - 20) then
if tonumber(currentGear) - 1 > 0 then
currentGear = tostring(tonumber(currentGear) - 1)
end
end
print(currentGear)
driverSeat.MaxSpeed = gears[currentGear]
end
task.spawn(function()
while true do
shiftGears()
task.wait()
end
end)