Background moving train

Here is the situation, I want a background train for my game, I want the train to despawn when it comes to the end of the track and spawns at the starting point automatically. so how should I script it? (P.S: The train I use is made by union and parts)

(It is just some photoshop magic, I don’t know how to describe it so I just made a gif.)
(something like this)
V V V V V V V V

moving-train

6 Likes

It is just some photoshop magic, I don’t know how to describe it so I just made a gif.

Ok, so personally, i will use tween, and for the spawning and despawning, for spawning you can use function that will fire itself on its end, and and for despawning, detect if the tween is completed and if yes, just delete it.

btw, off-topic tip, if i solved your question, mark it as solution, and if not, answer, what is problem with it.

Hi! You could also use CFrame:Lerp() in a loop to change the position of the primary part of the train (which should be Anchored). When the train reaches its goal, you set it back to its original position.

3 Likes

Great idea! I forgot to say, to say, that you can also set repeat on the twean to math.huge. Thx

So as a lot of people are suggesting a lerp/tween which is a good idea. However, those will only really work with either a lot of control points or if the track is going in a straight line. Can you further clarify that?

1 Like

For tweening you’d have to tween every part individually so wouldn’t this lag in some way?

Lerping would however be smoother cause you can set the CFrame of the primarypart.

Put the train in ServerStorage or ReplicatedStorage, and write something like this in a code:

local ServerStorage = game:GetService("ServerStorage")
local Train = ServerStorage:FindFirstChild("Train")

while game.Workspace.Train ~= nil do
    Train:Clone()
end

Put a block at the end of the track, which destroys the train, and that’s it i guess, also, put the train in the start.

Or use CFrame, if that doesn’t works, there are some scripts in the Toolbox which are CFrame for models. You can set up the script and might work. :+1:

2 Likes

What do you thing, that is setPrimaryPartCFrame doing? It is same, for smooth move you must weld it, or union it, or import it as single mesh.
btw, you can tween int and in loop set its value to part

I don’t know if you’re about to make the train move on the server too but personally, I would make the train move on the client. It will be smoother when using RenderStepped for the wait time.

1 Like

You’re right, I just tested it myself. Last time I most likely messed something up.

Or you can use bodyVelocity (i dont recommend body force, bec it is less controlable), bec when you set its maxforce to inf,inf,inf, it will do what you set there, and is less laggy.

In Toolbox i repeat, there are also CFrame scripts for models which are nice. But they are using a loop and cannot be stopped unless you make another script trying to fight that force. I need to explain more about what im talking?

Also, about only be moved in the client, i think you can use LocalScripts and that stuff.

You use TweenService. So first you would make a train model and set a primary part, then you just have to insert this script:

local TS = game:GetService("TweenService")
local TI = TweenInfo.new(time to go from one side to another, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

local Train = --reference the train

TS:Create(train.PrimaryPart, TI, {Position = Vector3.new(ending position))
1 Like

It is okay for it to only go straight line. I just don’ t know how to write a script and where should the parts name and where should put the script and the train.

The script above seems perfect for your question.
You only have to reference the time and the train model inside the workspace.

For example: if your train was named “Train” and you placed it in workspace, you would access it like

local TS = game:GetService("TweenService")
local TI = TweenInfo.new(10,  Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)

local Train = workspace.Train

TS:Create(Train.PrimaryPart, TI, {Position = Vector3.new(ending position));

Used the script from above assuming the train is in workspace named “Train”, moving in 10 seconds from the start to end position.

So, where should I place the script?
Should I change the parts name?

Should I leave my train model here for you guys to figure out how should it work?

Excuse me, but how can I set a primary part? (Yes I’m stupid and sorry about it)
P.S Here is a pic of a similar train model that I am working on, not sure can it help you or not.

Hi. You can manually choose the primary part by setting the model’s PrimaryPart property. Just click the model, press the PrimaryPart value (nil by default) and select a part parented to the train.

If you want to use a script, something like this would work:
workspace.Train.PrimaryPart = workspace.Train:FindFirstChild("Part")

1 Like