The best way to move Blocks with different directions at the same time

Good day !, I’m trying to move 3 blocks at the same time but with different directions (all in one script), I really tried with many things coroutine, functions and many more things but I could not do it correctly the way I could do it is put a script in each block but I do not like that. I think there is a great way to do it but I am not aware if someone could help me how to do it I would really appreciate it.

Note: It is a movement with Effect I used the for loop and vector3 to make a smooth movement effect, although I could also use: Lerp () but more precisely I came out with for What do you recommend?

Thanks for your attention.

Script that I made:

local Part = script.Parent

while true do
for i = 9, -51, -1 do

		local MoveToPosition = Vector3.new(i, Part.Position.Y, Part.Position.Z)
		Part.Position = MoveToPosition
		wait(0.001)
	end
	
	wait(1)
	
	for i = -51, 9, 1 do
		
		local MoveToPosition = Vector3.new(i, Part.Position.Y, Part.Position.Z)
		
		Part.Position = MoveToPosition
		wait(0.001)
	end

wait(1)
end`

1 Like

I highly recommend looking into Tween Service, Tween Service

1 Like

It is confusing what you are trying to accomplish, but this might help.

local Part1,Part2,Part3 = ...

local tweenInfo = TweenInfo.new(
	2, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

TweenService = game:GetService('TweenService')

TweenService:Create(Part1, tweenInfo, {Position = Vector3.new(0, 30, 0)})
TweenService:Create(Part2, tweenInfo, {Position = Vector3.new(0, 40, 0)})
TweenService:Create(Part3, tweenInfo, {Position = Vector3.new(0, 50, 0)})

Edit: Coroutines removed since tweens don’t yield, thanks for reminding me @snorebear

6 Likes

Because tweens don’t yield, the coroutines aren’t needed.

1 Like

when the interpolation ends at the indicated position when I activated the option “Reverse” (true) It does not wait for 1 second and returns to the position it was in. How can I make him wait a few seconds and then go back to where he was?

I’m afraid you will have to create two separate tweens (one forwards, other backwards), and play them sequentially with appropriate wait() between them.

1 Like

You would record the position at the start, then when the tween is over you would wait a few seconds and tween it back to that recorded start position.

1 Like