Converting a BodyVelocity Train to a LinearVelocity Train

Hi everyone, I usually ran a server with trains that runs based on BodyVelocity.
One thing I noticed is, it is starting to get laggy and a bit unstable, and I’d like to convert to LinearVelocity.

Below is the current script which uses BodyVelocity:

for _,v in pairs(bv) do
	v.Velocity = (v.Parent.CFrame * CFrame.Angles(0,math.rad(90),0)).LookVector * (-speed*.9)
	v.Velocity = Vector3.new(v.Velocity.X,-1,v.Velocity.Z)
end

Where speed is the initial speed of the train. (should be between 0 to 100), and it runs on a while loop per 0.1 seconds.

Does everyone have any idea on how to convert the system? Many thanks.

1 Like
-- Uncomment to mass-remove BodyVelocities from trains
-- for _,v in pairs(workspace:children()) do
--   if v:findFirstChildWhichIsA("BodyVelocity") then
--     v.BodyVelocity:destroy()
--   end
-- end

for _,v in pairs(workspace:children()) do
   if not v:findFirstChildWhichIsA("LinearVelocity") then
     local lv = Instance.new("LinearVelocity")
     lv.Parent = v
   end
end

CALCULATIONS

Velocity = (trainCFrame * CFrame.Angles(0, math.rad(90), 0)).LookVector * (-speed * 0.9)
1 Like

I don’t think it has a property caused Velocity for LV, should I enable something before adding the script?

1 Like

You have to set an attachment in the main part that moves the train and you have to connect the linearvelocity to that, and for my train game I have it set to linevelocity which is just a number

2 Likes

Should I set both Attachment0 and Attachment1?

1 Like

Change RelitiveTo to something else (I can’t remember what) and it should just show attachment0 and then you only need to set that one.

1 Like

Heres my properties. Whatever line velocity is, thats how fast the train will go.

image

1 Like

It worked nicely, thank you very much!

1 Like

You’re welcome!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.