What is the best way to script a train?

Hello everyone,

Recently I’ve been working on several games that contain a lot of train lines, and I’ve scripted a lot of train systems that work in different ways from TweenService to BodyVelocity (look in Train Images).

Train Images

Trains build by Macovei2057 VVV
image
image
Trains build by me VVV
image
image

However, every one of these has its own disadvantages, especially when it comes to curved rails/Tracks.

For example:

  • TweenService is not smooth when it comes to curved tracks, in other words, it looks odd when it moves from 1 checkpoint to another. + it setting it up is hard and takes a lot of time (look in Train Tween).

  • Body Velocity is a lot smoother when it comes to curves, however, there is always a chance that it might get the train stuck in the middle of the way, in other words, it’s not stable since it uses Roblox physics which is not the greatest to work with.

Train Tween

I am looking for the best way to script a train, a way that is stable like tweenService, but also smooth when it comes to curved/ angled tracks/rails.

Note: I am NOT an official scripter, im still learning.

If you have any idea how to improve one of these, or perhaps you recommend a new type of system then please let me know.

Thank You.

3 Likes

You can use Enum.EasingStyle.Linear to make it go smooter maybe try?

1 Like

Why not Sine? It’s much better.

when I made a plane, It was not so smooth but it got smooth when I use linear so I am telling him to use it

Hmm, for working with curved tracks why not just directly manipulate the train through CFrames and RenderStep?

You find the change in position path along the curve using Bezier curves over time and hence the movement of the train in a curvy manner which should fit your criteria for curved rails/Tracks.
https://developer.roblox.com/en-us/articles/Bezier-curves

For the orientation, that would be a bit more difficult but possible if you work with raycasting to obtain the floor’s normal, and using the tangent of the bezier curve path (by obtaining points on the curve close to each other).

Moreover, CFrames have been known to be reliable as they directly change the position property of the train like TweenService with the downside being the physics of the train. For that you will need to rely on doing physics detection for the CFrame’d object like a wall stick controller if you want players to ride the train as passengers.

1 Like

This seem quite useful, thx for that.

I was thinking of making the train only move to the direction it faces, with tween service. So whenever the orientation changes, right or left, the train will change its movement direction to the point that its facing.

Do you think that something like that is possible?

The tween script is already set to linear

Oh okay, well I agree to you on TweenService not being smoother on curved tracks.