How to make a basic train

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.

5 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

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