Train moving script help

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Train does not work the way I want it to work

  2. What is the issue? Right now the train breaking system works but its to fast and, then when you go forward it keeps going when you take your finger of w and does not level off.

bin = script.Parent
eng = { }
st = bin.VehicleSeat
msp = script.MaximumSpeed.Value
st.MaxSpeed = msp
mrs = script.MaximumReverseSpeed.Value 
spt = script.SpeedPerThrottle.Value 
mxt = math.ceil(msp / spt) 
crt = 50 
csd = 0 
cdr = 0 
drive = false
plr = nil 
DirTable = {"Reverse", "Neutral", "Foward"}
TrtStp = 0
function GetEngines() 
	for _, a in pairs(bin:GetChildren()) do
		if (a.Name == "Engine") then

				local bv = script.BodyVelocity:Clone()
				bv.Parent = a
				eng[#eng + 1] = a

		end
	end
end 

function GetDirString() return DirTable[cdr + 2]; end

function GetSpeedString(a)
	if (a < 0) then return "-"..a.."" end
	return msp
end

function GetPlayer()
	local a = st.SeatWeld.Part1
	if a then
		local p = game.Players:findFirstChild(a.Parent.Name)
		if p then
			return p
		end
	end
	return false
end

function GetHint(p)

	local a = p.PlayerGui:findFirstChild("TrainDriveGui")
	if a then return a; end
	return
end

wait(1)
GetEngines()

h = script.TrainDriveGui:Clone()
h.TextDisplay.Text = "Direction: "..GetDirString().." - Throttle: "..crt.."/"..mxt.." - Speed: "..GetSpeedString(csd).."/"..GetSpeedString(mrs)
h2 = h:Clone();

st.ChildAdded:connect(function () 
	drive = true 
	
end)

st.ChildRemoved:connect(function () 
	drive = false
	if GetHint(plr) then 
		GetHint(plr).Parent = nil 
		h = script.TrainDriveGui:Clone()
		h2 = h:Clone()
	end
end)

while true do
	wait(0.05)
	if drive then
		local p = GetPlayer()
		if p then
			plr = p 
			local gh = GetHint(p)
			if not gh then h2.Parent = p.PlayerGui end
			local h2 = GetHint(p)

			local trt, dir = st.Steer, st.Throttle
			if (TrtStp > 0) then
				TrtStp = TrtStp - 0.5
			else
				if (trt == -1) then
					if (crt > 0) then crt = crt - 1; TrtStp = 1; end
				elseif(trt == 1) then
					if (crt < mxt) then crt = crt + 1; TrtStp = 1; end
				end
			end

			cdr = cdr + dir
			if (cdr > 1) then cdr = 1 end
			if (cdr < -1) then cdr = -1 end
	
			if (crt == 0) then
				if (csd < 0) then
					if (csd + 0.5 > 0) then
						csd = 0
					else
						csd = csd + 0.5
					end
				elseif (csd > 0) then
					if (csd - 0.5 < 0) then
						csd = 0
					else
						csd = csd - 0.5
					end
				end					
			end
			if (crt > 0) then
				if (cdr == -1) then
					if (csd - (crt / 10) < (spt * crt) * -1) then 
						csd = (spt * crt) * -1
					else
						csd = csd - (crt / 10)
					end
					csd = csd - (crt / 10)
					if (csd < (spt * crt) * -1) then csd = csd + 0.5 end
					if (csd < (mrs * -1)) then csd = mrs  * -1 end
					if (csd > 0) then csd = csd * -1 end
				elseif (cdr == 1) then
					if (csd + (crt / 10) > spt * crt) then 
						csd = spt * crt
					else
						csd = csd + (crt / 10)
					end
					if (csd > spt * crt) then csd = csd - 0.5 end
					if (csd > msp) then csd = msp end
					if (csd < 0) then csd = csd * -1 end
				end
			end
			for _, a in pairs(eng) do
				a.BodyVelocity.velocity = a.CFrame.lookVector * csd
			end
			h2.TextDisplay.Text = "Direction: "..GetDirString().." - Throttle: "..crt.."/"..mxt.." - Speed: "..csd.."/"..GetSpeedString(mrs)..""
		end
	end
end

any help would be really helpful i’m not that advanced with scripting so the script is not really made well

2 Likes