How should I go about making this tween

How would go about making a train tween. Basically, I want to make a train tween along this path, however, I need it to play multiple tweens for it to follow a path as show in the image below

T = Tween and the Red Box is the train

For the 1st train, it would start too play tween1 first, then tween2 then t… untill it finishes

Tween1:Play()
Tween1.Completed:Wait()
Tween2:Play()
Tween2.Completed:Wait()

However, there would be another train, let say spawn at the part which it needs to play Tween2 first instead of tween1, unlike the first train, meaning i would have to change the tween sequence too

Tween2:Play()
Tween2.Completed:Wait()
Tween3:Play()
Tween3.Completed:Wait()

However there are many trains and i think there is a better way to change the tween sequence for evevery single train.

I know this problem is confusing and i would be happy to clarify any doubts

1 Like

It takes a little more tasks to put all the train scripts together, but the idea of shortening the code section of the tween is to use the following code.

--You need to define more at least TRname and Tweens and so on
local startnum = 1
local allpathnum = 8  

local function defineTRName()
	while true do
		if TRName == "Train" .. startnum then
			break
		else
			startnum += 1
		end
	end
end

local function tweenTrains()
	local currentnum = startnum
	
	while true do
		local currenttween = Tween .. currentnum
		
		currenttween:Play()
		currenttween.Completed:Wait()
		
		if currentnum == allpathnum then
			currentnum = 1
		else
			currentnum += 1
		end
		
		if currentnum == startnum then
			break
		end
	end
end

defineTRName()
tweenTrains()

sorry i cant undedrstand the code, could you help explain it abit. thxx

1 Like

I will only explain tweenTrain() since the rest is about definitions. tween1,tween2,… is TweenX(X=currentnum|first currentnum is strartnum as Train number). What we want to do is to tween and then tween the next number of tweeners, so after tweenX, we can add 1 to X (if X=8, we set X=1). Then when the train goes around and X becomes Train number (=startnum), you are done. This is what tweenTrain() does.

If you don’t know what you want to do and how you want to make it a function, write a flowchart. It is a bit tedious but should be useful!

the picture below is a flowchart of tweenTrain().
draft→generalize→functionize
Y is currentnum in the code.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.