How to make a basic train

Thanks for the simple tutorial really helpful! I think I’m going to use run service for the script.

1 Like

Thanks so much for this tutorial! I’ve been trying to make a self driving train for 3 days now. I tried using constraints but it was too complicated but this tutorial was perfect! :grinning: I really appreciate it.

I made many self driving vehicles, and I can help you make it

Thanks for the support i’ll contact you if I need to!
:grinning:

1 Like

Would it be possible for me to add on to this script so the train would move on a player’s command? such as when the player presses “w” the train would go forward and “s” to stop

2 Likes

Yes its possible, just do this

locan traincanmove = true

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

And make a function, which changes the variable traincanmove

3 Likes

Great tutorial but I’d recommend using task.wait() instead of wait().

1 Like

While it works very well, the only issue i have is that whenever the train becomes unstable, it just starts, well, flying around? Keep in mind that this is a simulated event- not an actual occurrence of the issue.
See attached video for what i mean.

robloxapp-20220419-2156335.wmv (1.8 MB)

You could try to raycast, if the bottom of the train is a track, if its a track, the train gets force to move, and apply some downward force, so the chances for derailing is less, and even if it de-rails, it does not move

1 Like

set the Velocity to Not affect the Y axis, something like this:

 script.Parent.AssemblyLinearVelocity = vector3.new(script.Parent.CFrame.LookVector.X*-15, script.Parent.Velocity.Y, script.Parent.CFrame.LookVector.Z*-15) 

Haven’t tested this yet and made it in a rush so probably can be improved upon

3 Likes

Did you try my model? It’s pretty stable up to very high speeds.
I took Woodx’s model and fine tuned it, using a VectorForce instead.

Just script a VehicleSeat’s Throttle input (W, up arrow or S, down arrow) to change the VectorForce’s X value.

2 Likes

every time I run it, the wedge falls through the track even though im using the WeldConstraint on the base of the train. How do I fix this problem?

Could you explain it please? I did not understand what u mean by this

you said cancollide has to be set to false, so what happens is that means it cant be collided with, so the wedge falls off the base and through the train ties. then that causes the base to drift left and derail. so how do I fix this problem?

1 Like

No no, I just said make the decorations of the track CanCollide = false, because those decorations might hit the train and it might de-rail, so I told to make it can collide false, you shouldn’t do cancollide = false for any train part, and make sure your part0 part1 of your train are kept properly

thats what I did on the Decorations

Yes. but using the weldConstraint on the wedges on the base of the train. is what is making it fall. do they need to be grouped with the base? because in your picture it shows that its not grouped together.

No you just have to stick it with weld, I don’t know why they are falling, make sure you kept it correctly

Okay, I will try that. if it does work. then that will help a lot

Firstly, you’re mistaking Velocity with Force.

Secondly, you’re setting AssemblyLinearVelocity directly, which is a bad practice to begin with.
(you should use LinearVelocityConstraint instead)

You’re also using a while wait() loop with it, which does not guarantee that the velocity will be updated every physics frame (which it should, and with this solution it probably won’t).
(This is also why it isn’t moving when you set 5 studs velocity as the speed will be lost in friction before the script will update it again)

Thirdly, you’re using Wedges, which have a more complex collision model than Parts. Why not just use a Part that is rotated 45 degrees?

1 Like