# Quick question, how can I sync loops?

So, I’m trying to sync loops since I have a graph that uses multiple loops to move lines across it. The problem is they don’t move exactly in sync. Heartbeat seems to work okay, but it’s way too fast. Any ideas? (I want a 1 second interval in between each time it moves).

Suggestion: try to put everything into the same loop instead of making different loops. This will save you a lot of time.

2 Likes

can we see how you’ve made the loops? it’s hard to give advice when there’s not much context given.

``````while true do
if template.Position.X.Scale < 0.8 then
template.Position = UDim2.new(template.Position.X.Scale + 0.01,0,v.Position.Y.Scale,0)
for i, x in ipairs(dupes) do
x.Position = UDim2.new(x.Position.X.Scale + 0.01,0, x.Position.Y.Scale,0)
end
if canDupe == true then
local dupe = tplate:Clone()
dupe.Position = UDim2.new(0.112, 0, v.Position.Y.Scale, 0)
dupe.Parent = template.Parent
dupes[#dupes + 1] = dupe
end
elseif template.Position.X.Scale >= 0.8 then
if canDupe == false then
for i, x in ipairs(dupes) do
if x.Position ~= template.Position and destroy == false then

x.Position = UDim2.new(x.Position.X.Scale + 0.01,0, x.Position.Y.Scale,0)
end
if destroy == true or dupes[#dupes - 1].Position.X.Scale == template.Position.X.Scale then

if destroy == false then
tableCount = #dupes

destroy = true

last = dupes[#dupes]
last.Position = UDim2.new(last.Position.X.Scale + 0.01,0, last.Position.Y.Scale,0)
--dupes[i] = nil
--x:Destroy()
end
if x ~= last then

tableCount -= 1
x:Destroy()
end

if tableCount <= 1 then
template:Destroy()
last:Destroy()

coroutine.yield("finished")
end
end
end
else
local dupe = tplate:Clone()
dupe.Position = UDim2.new(0.112, 0, v.Position.Y.Scale, 0)
dupe.Parent = template.Parent
dupes[#dupes + 1] = dupe
end
end
wait(1)
end
``````

prob not most efficient but it works except for moving all lines at once
https://gyazo.com/7ab416314aa4fe5819f8d82537b5f778

ohh okay, and do you have multiple loops like that or is that the only loop?

well mutiple, because one runs for each line created.

my recommendation is you use object oriented programming to create lines. for example, you could have a ModuleScript that looks like:

``````local Line = {}
Line.__index = Line

function Line.new(Template) -- this should contain anything to personalize the line
local self = setmetatable({}, Line) -- creates a new Line object with the Line functions
self.Template = Template
return self
end

function Line:Update()
-- this function has stuff that updates the line. for example
self.Template.Position = self.Template.Position + UDim2.new(0.1, 0, 0.1, 0)
end

function Line:Remove()
-- add a function that removes everything for the line
end

return Line
``````

then, in a separate script that controls everything, you have a loop that constantly runs every second. you also have a table that contains all the lines you created. now all you have to do is this:

``````local LineCreator = require(script:WaitForChild("Line")) -- the module script you made
local Lines = {} -- will contain all the lines you create

spawn(function()
while wait(1) do
for i =1, #Lines do
Lines[i]:Update()
end
end
end)

-- to create new lines, you simply do
local NewLine = LineCreator.new(Template) -- insert the template or whatever you need to create a line
table.insert(Lines, NewLine) -- add the line to the table

-- to remove a line, simply remove it using table.remove()
local Line = table.remove(Lines, 1) -- removes the first line from the line table
Line:Remove() -- removes the line for good
``````

you can read up on object oriented programming here, i think this is one of the cases where it’s really useful.

2 Likes