The simplest solution is to make your conditions impossible to run at the same time, i.e., mutually exclusive. This is easily accomplished by changing if to elseif (and removing the previous end).
local driving = 0
if script.Parent.Driving.Right:GetState() and not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
driving += 1
--CAR CODE HERE
--> change if to an elseif
elseif script.Parent.Driving.Left:GetState() and not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
driving += 1
--CAR CODE HERE
end
However, the professional approach is to have direction be a numerical value. This lets your code be a lot more flexible and simplified going further.
local driving = script.Parent.Driving
local left = if driving.Left:GetState() then 1 else 0
local right = if driving.Right:GetState() then 1 else 0
-- If player presses left, then it's 1, if player presses right, it's -1, if player presses both or neither, it's 0
local direction = left - right
With this direction value, doing rotation (or whatever you need to do) is much simpler.
car.AssemblyAngularVelocity = CFrame.fromEulerAngles(direction, 0, 0) -- A very basic example
Et cetera
You’re duplicating the same condition twice. Just put that condition outside and wrap everything.
local driving = 0
--> These conditions apply to both states, so move them out
if not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
if script.Parent.Driving.Right:GetState() then
driving += 1
--CAR CODE HERE
elseif script.Parent.Driving.Left:GetState() then
driving += 1
--CAR CODE HERE
end
end
Also, you duplicate the same code for driving (I assume), which can introduce copying errors. This was already addressed before, but assuming you choose some other direction, consider this structure, which places the duplicated code outside.
local driving = 0
if not MainGame.CurrentState.GameOver and MainGame.CurrentState.Fuel > 0 then
if script.Parent.Driving.Right:GetState() then
driving += 1
elseif script.Parent.Driving.Left:GetState() then
driving += 1
end
if driving then --> Car code is deduplicated. Easier to maintain.
-- CAR CODE HERE
end
end