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)```