Roblox Locomotive Velocity Error

local Seat = script.Parent
local Train = Seat.Parent
local Body = Train.Body
local LBogie = Train.LBogie
local RBogie = Train.RBogie
local FCoupler = Train.FrontCoupler
local BCoupler = Train.BackCoupler
local vel = script.Parent.Parent.Body.BodyAngularVelocityVelocity
local Velocity = 0

speed = 100
acceleration = 0.1
deceleration = 0.2

Seat.Changed:connect(function(type)
	if type == "Throttle" then
		local Input = Seat.Throttle
		repeat
			Velocity = Velocity+Input
			if Input*Velocity/math.abs(Velocity) == -1 then
				wait(1/script.Deceleration.Value)
			else
				wait(1/script.Acceleration.Value)
			end
			if Velocity > script.MaxSpeed.Value then
				Velocity = script.MaxSpeed.Value
			elseif Velocity < -script.MaxSpeed.Value then
				Velocity = -script.MaxSpeed.Value
			end
		until Seat.Throttle ~= Input
	end
end)

while script.Parent ~= nil do
	wait()
	vectorpower = script.MaxPower.Value*Seat.CFrame.lookVector
	vel.MaxTorque = Vector3.new(vectorpower.X>0 and vectorpower.X or -vectorpower.X,
		vectorpower.Y>0 and vectorpower.Y or -vectorpower.Y,
		vectorpower.Z>0 and vectorpower.Z or -vectorpower.Z
	)
	vel.Velocity = Velocity*Seat.CFrame.lookVector
end

I am having issues with this where the locomotive just levitates in the air, this might also be bc of the properties on it but if it is, please let me know.

1 Like

Try replacing

vel.Velocity = Velocity*Seat.CFrame.lookVector

with

local targetVelocity = Velocity*Seat.CFrame.lookVector
vel.Velocity = Vector3.new(targetVelocity.X, vel.Velocity.Y, targetVelocity.Z)

This is the quickest and dirtiest fix you can do. You’re setting the train’s velocity based on the facing of the seat. If the seat is facing forward, the Y velocity is 0 and your train will always have 0 Y velocity meaning it won’t go up and down, hence levitation.

Thank you, but I have another issue where when the locomotives connect they fly off the tracks.