A way to do multipe tweens in the same loop?

I’ve used a single loop for every tweening part, but I’ve realized that it wasnt the best way to do it.
Here’s an example of it:

function move(part)

   local CurrentNode = 1

   while true do
      if CurrentNode < #Nodes:GetChildren() then
         --do tween stuff here
         if ChangedSpeed == false then
            CurrentNode += 1
         end

      end
end


eventConnection.Event:Connect(function()
   move(currentPart)
end)

if anyone knows how to improve this system tell me!

other info:
-all the parts do the same route
-all the parts do different things when on a specific node

1 Like

What’re you trying to do? I don’t really get it. Can you elaborate please?

(If it already works you should probably move this topic to #help-and-feedback:code-review)

oh sorry if I didnt put much here. Basically that’s a function that moves by tween a part through each node of a nodes folder (ex. in the folder there are 10 nodes, so the part will do a tween for each node until reaching the end node (in this case node 10), then do the same again (start on node 1) and repeat)
(thanks for making me know the code review topic)

So, if you wanted to iterate over all of them, you could do something like this:

for _, node in ipairs(Nodes:GetChildren()) do
    --tween stuff here
end

Then, you could wait a certain length of time, and also put the entire thing in a while true loop.

actually that cant be a fix (when speed changes the tween needs to resume), I need a way to dont use a while loop for each part (if there are 20 parts there are 20 loops, and I have 400+ parts to move)

function move(part)

   while true do --I need to use a single loop for every part
      --stuff

(also there is a wait for every tween completed)

Its really hard to get what do you want.

Sinse it doesnt look like ure really moving a part, just increasing a value that might represent position, this might help:

local partsMoving = {}
local partsNode = {}

local speed = 1
local maxnodes = 10

task.spawn(function()
    while true do
        task.wait()
        for _, part in pairs(partsMoving) do
            partsNode[part] += speed
            if partsNode[part] > maxnodes then
                partsNode[part] = partsNode[part] - maxnodes
            end
        end
    end
end)

StartMovingPart.Event:Connect(function(part)
   partsNode[part] = 1
   table.insert(partsMoving, part)
end)
1 Like

I’m sorry, but that cant work since the nodes have different distances and so, different time they will use to reach the next node. Actually its a good idea to use a value to say its position. but how to know the position of the place by a number (ex. totalDistance = 50, totalNodes = 10, currentPos = 25, how I know where the part might go? maybe some [Dis/Nodes] and other?)

Are you trying to make it so the tweens change their speed based on the distance from each node the part moves to?

same speed, so different time (distance/speed)

Do a loop and then make a new tween info based on the distance between the current node and the next node

well, I decided to do a system in a different way. Basically there is a single value that changes, then all parts find their position in the nodes, then lerping on how distance there is from the next. then when this value is changed, this is repeated again. This make sure to evitate spacing issues and bad sync for every client. But still, thank you all for the responses!

1 Like

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