Alternative to Body Movers

Hello! I have a train and I move it with BodyVelocity by setting the velocity every 0.2 seconds in a while true do loop in a server script. The problem is that every train has 2 scripts and if there are 15 trains - that’s gonna be 30 scripts in total, so it will memory intensive and thus, the server will lag.

So, straight to my question. What is the best alternative to BodyVelocity that will not take so much memory and won’t be too hard to use?

Any help is greatly appreciated!

BodyVelocity is a good method of moving models in your game, however perhaps you’re going around the scripts wrong.

Is it entirely necessary to update the velocity every 0.2 seconds?
Could you not create a module that handles all trains? For example, listen for a model being added to a folder and handle the train there.

Hope this helps,
-Tom :slight_smile:

1 Like

Is it entirely necessary to update the velocity every 0.2 seconds?

Well, it is kind of necessary since I want the speed to go up smoothly and so, I have to wait in between the different speeds.

Could you not create a module that handles all trains? For example, listen for a model being added to a folder and handle the train there.

I was thinking of controlling all trains from 1 script but I heard that when a script gets a lot of requests, it can eventually timeout. (which I don’t know if it’s actually true)

In terms of speed moving up slowly, have you tried using the TweenService?

In terms of timing out, that isn’t an issue you need to worry about with scripts. One of the issues you will need to worry about is scheduling and making sure all your code is being executed. You should consider using coroutines to make sure everything’s running smoothly.

For reference and further help, consult the following articles and documentation:

Hope this helps,
-Tom :slight_smile:

have you tried using the TweenService?

I am not really experienced with tweening whole models and I don’t know how to calculate the distance to the next node and tween each carriage respectively.

In terms of timing out, that isn’t an issue you need to worry about with scripts. One of the issues you will need to worry about is scheduling and making sure all your code is being executed. You should consider using coroutines to make sure everything’s running smoothly.

I will consider this and will try doing that today. Thanks for the help!

You can tween the Velocity attribute of the BodyVelocity object under one of the parts in the model, it’s actually pretty simple:

-- Void : Accelerates your train
-- BodyVelocityObject : BodyVelocity Object
-- EndVelocity : Vector3
-- Time : Float
function Tween(BodyVelocityObject, EndVelocity, Time)
     local TweenInfo = TweenInfo.new(Time)
     local Tween = game:GetService("TweenService"):Create(BodyVelocityObject, TweenInfo, { Velocity = EndVelocity })
     Tween:Player()
     Tween.Completed:Wait()
end

Hope this helps,
-Tom :slight_smile:

1 Like

I will try this and will get back to you ASAP. I will mark it as a solution for now. Thanks for the help! :slight_smile:

1 Like