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.
-- 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
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