Good cframe-based train moving system?

Alright so let’s say we have a train model that uses cframe to move.

I’ve been trying to create a good cframe-based train moving system, but no luck, all jittery, train cars separating. I don’t even know whats causing all of this. Maybe something was needed to be changed.I don’t know.

I have no idea how do i make a good train moving system. I tried applying people’s suggestions to me, but still no working. Have no luck.

I tried searching for other topics of how to make a good train moving system but no results from google. Surf on the internet, useless information, only videos of trains using physics like using conveyor belts to move them, you know. Those other useless informations like no scripting included, only physic-based constraints.

This is what a good system works:
https://gyazo.com/bcd59c6d43a08f82cd57bfdb6f1d8c74

Got that footage from my friend.

Edit: Forgot to mention that as you can see in the footage, the train carts or wagons are not even separating! They maintain position. All moving together, synchronized and perfect!

Anyways, got something that could help me? Reply below! I will try it! Something to suggest code to me? Feel free! (Note: I’m not asking for code, just asking if you have some code to suggest for me)

Anything will help! Once again, thanks in advance!

3 Likes

If you are dealing with raw CFrame values to move it, definitely use TweenService as a for loop will cause lag as the :wait() function is not going to be every frame (lag happens). I would suggest using constraints as that is really what these big train games use. They will modify CFrame values in the sense that they act on an object (or objects) using forces and constraints (hence the name) and thus the object moves like it would in real life (the position changes).

Check out this article: https://developer.roblox.com/en-us/api-reference/class/BodyForce/index.html
and the other force articles for an intro to it. Cheers.

1 Like

How did you make your system? If you move the train every physics frame, it shouldn’t be jittery. I use a different approach, using TweenService and some physics, to make sure it works well in combination with player characters. It uses two “gliders” that are tweened along the tracks, with AlignPosition Constrains to keep the train itself above them.

2 Likes

it’s better to :Connect() to the RunService.Heartbeat than to :Wait() on it. This makes sure you always trigger on every frame.

These large train-oriented games (Stepford County Railway, to name one) do not use BodyForce. Instead, they use glider parts (very different to the ones I described in my earlier post) that follow the tracks, weld the train carriages to the gliders, and use a BodyVelocity to set the train’s speed.
Source: am train developer myself.

BodyForce does not work very well, because it does not let you control speed.

3 Likes

This should work for ya.

1 Like

This is a very different problem. The post you linked is trying to keep a player on top of a CFrame-moved train. The problem in this post is making that CFrame-moved train work smoothly.

1 Like

@Strongjohnfgamer I got that already, I have fixed my player standing on train script.

1 Like

@thenoobforevernever But RunService.Heartbeat:Connect() can only be used in localscripts.

I have quoted something cause I forgotten something to mention, just edited the post 1.

Heartbeat isnt only for local scripts, and also you will always have “jittering” unless the train is animated on everybody’s client instead of depending on only the server’s result or using something like the tween v2 module on the dev forum where the train moves on all clients even when the tween is server initiated

3 Likes

Well I could try that. But didn’t you see in Post 1? I have added an edit on the bottom part somewhere. Here quoted it for you:

1 Like

@Wunder_Wulfe Would it prevent the train cars from separating? Would it also move the train cars together at the same time? Would it make them synchronized like Perfect?

1 Like

Well assuming that you use linear or near linear tweens / interpolation then they should not come apart. If you wanted to though, you could just make it some sort of node based track and and move the carts along the track manually

2 Likes

If you want a realistic train, just use a parameterized curve/spline to represent your track. Then, you can simply use the resulting function to find x, y, theta for any distance t along the track.

3 Likes

@Wunder_Wulfe Yup! I use nodes for the train to follow and where its going. Basically I weld all the trains parts of body to the primarypart. Then all of them unanchored except the part that is going to be tweened. I tween with Linear, using cframes to adjust position and rotation so I wouldn’t have to update it with vector3 separately.

I think using the algin direction and algin rotation to tracks may reduse the lag very much

1 Like

Hello, this is a late reply but i hope this tutorial i found would help you!

1 Like