Cart deceleration bug?

I am currently making a cart game that sort of uses the classic cart ride mechanics, however i am experiencing an issue i am not sure i can fix with the my acceleration and deceleration logic.

The Problem:

In this script, it will slowly make the cart reach up to its target speed rather than shooting straight away to the speed, like other cart rides. However, when the cart is decreasing and you then try to go forward it will stay at the last speed it was when decreasing. Heres a video as an example:

(The number displayed on the cart is its target speed, the number displayed in the output is the current speed)

The script:

local cart = script.Parent
local Seat = cart.VehicleSeat

local BackLeft = cart.BL
local BackRight = cart.BR
local FrontLeft = cart.FL
local FrontRight = cart.FR


local targetSpeed = 0
local currentSpeed = 0 

-- services
local runService = game:GetService("RunService")
local collectionService = game:GetService("CollectionService")

-- modules
local regenModule = require(script.RegenModule)
local explosionModule = require(script.ExplosionModule)
local soundModule = require(script.SoundModule)

--instances
local buttonFrame = cart.ButtonsFrame
local forwardButton = buttonFrame.ForwardButton
local backwardButton = buttonFrame.BackButton

local speedOMeter = buttonFrame.SpeedOMeter
local hasPowered = false 

local speedGUI = speedOMeter.SurfaceGui.TextLabel
speedGUI.Text = tostring(math.abs(targetSpeed)) 


local speedChangeRate = 1

------------------------------------------------------------------------------------
local function adjustSpeed()
	if not hasPowered then
		while currentSpeed ~= 0 do
			wait(0.05)
			print(currentSpeed)
			if currentSpeed > 0 then
				currentSpeed = math.max(currentSpeed - speedChangeRate, 0)
			else 
				currentSpeed = math.min(currentSpeed + speedChangeRate, 0)
			end

			FrontLeft.BaseWheel.HingeConstraint.AngularVelocity = -currentSpeed 
			FrontRight.BaseWheel.HingeConstraint.AngularVelocity = currentSpeed 
			BackLeft.BaseWheel.HingeConstraint.AngularVelocity = -currentSpeed
			BackRight.BaseWheel.HingeConstraint.AngularVelocity = currentSpeed

			speedGUI.Text = tostring(targetSpeed)
		end
		speedGUI.Text = tostring(targetSpeed)
	else
		while hasPowered and currentSpeed ~= targetSpeed do
			wait(0.05)
			print(currentSpeed)
			local speedDiff = targetSpeed - currentSpeed
			currentSpeed += math.sign(speedDiff) * math.min(math.abs(speedDiff), speedChangeRate)

			FrontLeft.BaseWheel.HingeConstraint.AngularVelocity = -currentSpeed 
			FrontRight.BaseWheel.HingeConstraint.AngularVelocity = currentSpeed 
			BackLeft.BaseWheel.HingeConstraint.AngularVelocity = -currentSpeed
			BackRight.BaseWheel.HingeConstraint.AngularVelocity = currentSpeed
			
			speedGUI.Text = tostring(targetSpeed)
		end
	end
end



local function onButtonPressed(amount)
	targetSpeed += amount
	speedGUI.Text = tostring(targetSpeed)
	if not hasPowered then return end 
	coroutine.wrap(adjustSpeed)() 
end


local function togglePower(button)
	hasPowered = not hasPowered

	if hasPowered then
		buttonFrame.LeverBase.Lever1.Transparency = 1
		buttonFrame.LeverBase.Lever2.Transparency = 0
		buttonFrame.LeverBase.ProximityPrompt.ActionText = "Brake"
		--coroutine.wrap(adjustSpeed)()
		adjustSpeed()
	else
		buttonFrame.LeverBase.Lever1.Transparency = 0
		buttonFrame.LeverBase.Lever2.Transparency = 1
		buttonFrame.LeverBase.ProximityPrompt.ActionText = "Forward"
		adjustSpeed()
	end
end
------------------------------------------------------------------------------------

forwardButton.ClickDetector.MouseClick:Connect(function()
	onButtonPressed(5)
end)

backwardButton.ClickDetector.MouseClick:Connect(function()
	onButtonPressed(-5)
end)

buttonFrame.LeverBase.ProximityPrompt.Triggered:Connect(function()
	togglePower(speedOMeter)
end)```
local function adjustSpeed()
	if not hasPowered then
		while currentSpeed ~= 0 do

Not sure, but whenever you decelerate and speed ~= 0 you seem to be stuck in this loop because it stays there until currentSpeed gets to 0.
Check inside the loop is the speed input is increased or decreased and if it is then break the loop.

Unless targetSpeed is an IntValue (only whole numbers) you may also want to change it from ~= 0 to is > -.1 and < .1 so decimals like .02 will break the loop as well.

im sorry i dont really understand on waht to do exactly, i dont want to sound like an idiot :confused:

Inside the loop you need to check to see if the accelerate or decelerate buttons have been pushed in the adjustSpeed function.
Otherwise the script gets stuck inside that loop and won’t do anything else until currentSpeed = 0.