Repeat loop before it's finished? (Or more efficient way to do this)

Hello. I am trying to make a light strip with many smaller lights, as you will see below. I made a script which gives them the following effect:

https://gyazo.com/11299832441e8b69d388b631f1b3fd79

Script:

while true do

	local frames = script.Parent:GetDescendants()






	for _, v in pairs(frames) do
		if v:IsA("Frame") then



			v.BackgroundColor3 = Color3.fromRGB(255, 68, 21)

			local TweenService = game:GetService("TweenService")

			local Info = TweenInfo.new(

				0.5, 
				Enum.EasingStyle.Sine, 
				Enum.EasingDirection.Out,
				0, 
				true,
				0

			)

			local ColorChange1 = TweenService:Create(v, Info, {BackgroundTransparency = 0})


			ColorChange1:Play()



			wait(0.01)



		end

	end

end

My question is how would I repeat the loop before it is finished so the effect is more frequent? so far it only repeats once the effect reaches the last light at the top. Thanks for any help :slightly_smiling_face:

You could put the for loop inside a function that is spawned every so often. How often does not depend on whether the last one has finished, then.

local function LightPatternStart()
	for _, v in pairs(frames) do
		--Rest of your original code
	end
end

while true do
	task.spawn(LightPatternStart)

	wait(1) --Can be any number even if the for loops would normally overlap
end
2 Likes

How about changing the code like this?
added a spawn function

while true do

	local frames = script.Parent:GetDescendants()

	for _, v in pairs(frames) do
		spawn(function()
			if v:IsA("Frame") then

				v.BackgroundColor3 = Color3.fromRGB(255, 68, 21)

				local TweenService = game:GetService("TweenService")

				local Info = TweenInfo.new(

					0.5, 
					Enum.EasingStyle.Sine, 
					Enum.EasingDirection.Out,
					0, 
					true,
					0

				)

				local ColorChange1 = TweenService:Create(v, Info, {BackgroundTransparency = 0})

				ColorChange1:Play()

				wait(0.01)

			end
		end)
	end

end
1 Like

This would work as well but spawns more threads.

1 Like

Actually, the best to thread is using coroutine.

1 Like

Thank you for the reply, but that basically does the same thing as before.

Are you sure its not the tweens themselves that are overlapping and limiting the speed?

1 Like

How would I be sure? Also, I changed the wait amount and the tween completion amount but the effect stayed the same.

I guess you could turn all the lights on at the end of LightPatternStart, if you dont see them all go on, that means theres no extra time between tweens. Im not sure thats the issue though.

1 Like

Okay my bad, your original reply seems to be the solution, however the effect only repeats once as a result of the code, and then goes back to how it was before.

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