How to make a basic train

Hello Developers,

I’m going to tell you how to make a basic train, which can move and even turn, here’s a video:


Now you see, the train can actually turn

:warning: This is a tutorial on how to make the basic train, which moves, not the train which you see irl
This is an easy tutorial, and it is based on basic scripting and Roblox physics

Step 1: Making the track

image
You need to make two blocks as a support, then you need to keep some space for moving

Step 2: Decorations

Make sure you make the decorations CanCollide as false, the train should not be hit by any obstacle, causing it to de-rail
image

Step 3 : Making train movement system

First make the base of train
image

Second, keep 2 wedges like in the image


This is for sliding the train and to avoid easy de-rails

Now, weld the wedges to the base, don’t anchor any part of the train
image

Make sure the base of the train faces the front side of train

Now, to avoid unnecessary orientation
Keep 2 blocks, front and back, and make them transparent, make sure to weld them to train base
image
image

Its just a block, make sure it has 1 stud distance from track
image

Now add a script in the base of the train
Script:

while task.wait(0.1) do -- a loop
    script.Parent.AssemblyLinearVelocity = script.Parent.CFrame.LookVector*-15 -- makes the force of the train, 5 studs per second
end

Now test it:

You can add more stuff on train, make sure you give upward force if the train is too heavy, and give more force to the train to move it faster, too fast = de-rail so be careful

Tutorial is done
Enjoy!

Support me

Paypal

96 Likes

Small little typo there, it should say “15 studs per second”.

while wait() do

It’s considered bad practice to do this. wait() returns a number, but while do should only accept conditions, aka true or false. Also, the more wait()s your game uses, the longer it actually yields the script, you should be using the new task.wait() instead. Also, apparently using task.wait() prevents your script from stopping if you use it, so you should keep that in mind.

3 Likes

This is bad advice to just blindly give without explaining the difference; they are not equivalent. An infinite loop using task.wait will not stop if the script is disabled or destroyed. A loop using wait will. You need to account for this if you use it.

9 Likes

That’s intended behaviour? I thought it was some sort of bug.

3 Likes

Yes it should say:
image
But 5 studs force is not moving, 15 studs makes the train move, 10 studs force is probably gone because of friction and gravity of the train

And as mentioned @PeZsmistic explained why you should not use task.wait

I did not know it either, but my point was explaining how to make a working train, rather than a scripting tutorial so I did not learn much about it

3 Likes

I am not a game designer so i do not know how to do the decorations. Great tutorial tho.

2 Likes

Thank you, the reason I made this tutorial, is there are not many games on Roblox, based on train system

There are a couple issues here.
If it’s too heavy just make the Parts close to Massless. Massless may have some physics issues if the entire train car is Massless.
What keeps the train on the track if it goes faster? I’d suggest having another Part under the track that you could use Collision Groups to keep that Part from colliding with anything other than the rails.

Are the WedgeParts in the center of the car and you have a block at either end? I see you said to keep the Blocks 1 stud from the track, but this would cause the car to pivot or twist back and forth in the Y axis on the tracks. A better way of doing it might be to have a square Part at either end of the car welded at 45 degrees to the main train par. The square Part whould be just slightly less (like .01 stud) diagonally than the distance between the track rails. This would allow the car to stay aligned, but not twist on the track.

6 Likes

Have you tried this with multiple “cars” linked together? This may work with that kind of setup, however, may also brake due to Roblox physics if the cars are linked using phisics constratnts.

Nope and you can keep a hinge constrasit which keeps rotating, and it works, I tried to do it, don’t worry its possible

Yes you can do that, but just to make sure this is a simple tutorial, which is easy and understandable, I did not make it complex using collision groups, however once you got to know how this basic train works, you can make your custom train, so I did not include the hard ones here

Look try to keep it full touched to rails, then you notice, the train won’t move
The reason, I told to keep 1 stud is, when changing track, or turning of train, the 1 stud space gets filled and then it turns smooth as you see in the video at last

Why not 2 studs space?
As you know weight = mass * gravity
Roblox physics have gravity and mass, now if you keep 2 studs gap, the weight on the right side of the train increases, center of mass chages, and the train de-rails, or turns backwards

You don’t actually have to have a script with any kind of loop in it.
Here’s an example place I put together, with a ‘circular’ track and 2 carriages connected by a RodConstraint.
The carriages have 4 wheels with HingeConstraints in them, as well as 2 axles below the track with HIngeConstraints, and 2 wheels between the tracks to keep the carriages aligned. All HingeConstraints have the AcuatorType set to ‘None’.

Both carriages have a VectorForce in them which doesn’t require constant updating with a script. You can put a script in to change the X value of the script to make the train faster or slower, but I kept it simple.

When you test the place hop on a seat and go into both VectorForces. They should have a Force of 0,-1000,0 (the Y axis value helps keep the carriages on the track.
Set the X value of both of the VectorForce.Force to around 500 and the train should start to move forward slowly. I’ve had both X Forces up to 40,000 and the carriages stay on the track! After that it starts to get stuck when the RodConstraint goes over center, or it’ll derail, but the speed is impressive, especially for the 20 degree sharp turns of the track sections.

If you open up the Collison Group Editor in the Studio Model tab you can see where I added the green axles to the Wheels group, and all the ties to the Ties group, unchecking the boxes to keep them from colliding with each other. If you click on one of the axles you’ll see in the Properties window that the CollisionGroupID is 1. If you click on one of the Ties and look in the Properties window you’ll see the tie’s CollisionGroupID number is 2. There isn’t any scripting required for this either since it’s explained in the Collision Filtering link.

Train Experiment.rbxl (39.8 KB)

9 Likes

What, uh the game link you posted, the train does not move at all

Just have to read every part…

2 Likes

That works, but its way too complicated mechanics, and I am not a builder, so I don’t know much about making trains and forces, so I made a basic train tutorial

Not really that complicated. All of us just need to try things that are more complicated than you are used to to learn how to do them.
I tried a few things while making this system, admittedly most of which I’d already used before in other aspects, but I also made some mistakes and discovering ways to overcome those difficulties helped me out in the long run.

3 Likes

Could you give me any source or where task.wait() wouldn’t destroy the loop upon script destruction/disablation?

1 Like

image

task.wait() does not destroy the loop

What he told is, when you run a loop based on task.wait() then if you destroy the script inside game, it will still be running, while if you use a while wait loop, it will not work if the script is destroyed

I know, but I wanna know where he originally get that source from.

Oh ok, next time dm him, so it wouldn’t lead to a confusion