Train moving script help

Hey forum i’m trying to make a hover train but when every I hit w it keeps going up and if I hit s it keeps going down and I want it to level off. If anyone can help me it would really help me.
I have also tried to fix it but made it worse so I reverted it

Sorry if the code is messy.

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 --throttle
csd = 0 --speed
cdr = 0 --direction
drive = true 
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

I have made a topic before a couple weeks ago but got no help from it so i’m trying once more to see if I can get help.

1 Like

Just put the script here by copy/pasting it but put ``` (three backwards single quotes) before and after the script.

1 Like

I know how to do it, just a lot but sure i’ll add it to it

I think your problem is that you don’t have if trt==0 then in the while true do loop

I’ll try that thanks for the tip.

1 Like

Tried that and did not work. Though thanks for trying!

Something like this?

elseif(trt == 0) then
     crt = crt 
1 Like

Ok I’ll try that when I get home

Tried that still no luck. any other ideas.

Heres the code now.

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 --throttle
csd = 0 --speed
cdr = 0 --direction
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
				elseif(trt == 0) then
					crt = crt
				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

If you want I can make a small game and show what I’m trying to do.