For i,v in pairs do loop problem

I wanted to make this spawn model with lights change color at the same time smoothly, the problem is that it changes one part at a time and i’ve tried so many ways but nothing won’t change. Here is the code.

local TweenService = game:GetService("TweenService")

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

local colors = {
	Color3.fromRGB(255, 0, 0);
	Color3.fromRGB(255, 85, 0);
	Color3.fromRGB(255, 255, 0);
	Color3.fromRGB(0, 255, 0);
	Color3.fromRGB(0, 255, 255);
	Color3.fromRGB(0, 0, 255);
	Color3.fromRGB(85, 0, 255);
	Color3.fromRGB(255, 255, 255);
}

Model = script.Parent["Spawn Model"]

while true do 
	
	for i,v in pairs( colors) do 	
		for i,Luci in pairs(script.Parent["Spawn Model"]:GetChildren()) do
			if Luci.Name == "LightPart" then
	
			local tween = TweenService:Create(Luci, tweenInfo, {Color = v})
			tween:Play()
			tween.Completed:Wait()
		end
		end
end
end

tween.Completed:Wait()

Just remove that line. It’s causing the code to pause until the tween is finished before moving on to the next one.

1 Like

The yield needs to be moved one loop out so that all tweens can be created and started before it happens. This should fix the issue.

while true do
	for i,v in pairs(colors) do
		local tween
		for i,Luci in pairs(Model:GetChildren()) do
			if Luci.Name == "LightPart" then
				tween = TweenService:Create(Luci, tweenInfo, {Color = v})
				tween:Play()
			end
		end
		if tween then
			tween.Completed:Wait()
		end
	end
end
3 Likes

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