For i,v in pairs() tweening issue

  1. What do you want to achieve? Keep it simple and clear!
    I’m trying to make a lighting system that changes the color of every part In a folder at the same time, everything works.

  2. What is the issue? Include screenshots / videos if possible!
    The tweens are “taking turns” on each part/union. So it goes to one first, then another, and another, so another. This is extremely inefficient and I thought this was gonna be efficient…

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried some different tween methods but nothing seems to really work any better.

Script:

local tweenservice = game:GetService("TweenService")

local info = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)

game.ReplicatedStorage.ColorChangerEvents.Change.OnServerEvent:Connect(function(plr, r, g, b, stafflock, groupid, minRank)
	print(r, g, b)
	local selColor = Color3.fromRGB(r, g, b)
	print(selColor)
	if stafflock == true then
		if plr:GetRankInGroup(groupid) >= minRank then
			for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
				if v:IsA("BasePart") then
					local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
					tween2:Play()
					wait(0.5)
					local tween = tweenservice:Create(v, info, {Color = selColor})
					tween:Play()
				end
			end
			for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
				if v:IsA("UnionOperation") then
					v.UsePartColor = true
					local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
					tween2:Play()
					wait(0.5)
					local tween = tweenservice:Create(v, info, {Color = selColor})
					tween:Play()
				end
			end
		end
	else
		for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
			if v:IsA("BasePart") then
				local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
				tween2:Play()
				wait(0.5)
				local tween = tweenservice:Create(v, info, {Color = selColor})
				tween:Play()
			end
		end
		for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
			if v:IsA("UnionOperation") then
				v.UsePartColor = true
				local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
				tween2:Play()
				wait(0.5)
				local tween = tweenservice:Create(v, info, {Color = selColor})
				tween:Play()
			end
		end
	end
end)

(Yes the event is fired from a local script the player activated with the gui.)

Video of how it’s currently working:

You need to make this a coroutine or use task.spawn to prevent the waits stopping going to the next part.

Where would I but task.spawn exactly? I’ve never used it before.

I believe putting it inside of every for loop should work.

local tweenservice = game:GetService("TweenService")

local info = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)

game.ReplicatedStorage.ColorChangerEvents.Change.OnServerEvent:Connect(function(plr, r, g, b, stafflock, groupid, minRank)
	print(r, g, b)
	local selColor = Color3.fromRGB(r, g, b)
	print(selColor)
	if stafflock == true then
		if plr:GetRankInGroup(groupid) >= minRank then
			for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
				task.spawn(function()
					if v:IsA("BasePart") then
						local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
						tween2:Play()
						wait(0.5)
						local tween = tweenservice:Create(v, info, {Color = selColor})
						tween:Play()
					end
				end)
			end
			for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
				task.spawn(function()
					if v:IsA("UnionOperation") then
						v.UsePartColor = true
						local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
						tween2:Play()
						wait(0.5)
						local tween = tweenservice:Create(v, info, {Color = selColor})
						tween:Play()
					end
				end)
			end
		end
	else
		for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
			task.spawn(function()
				if v:IsA("BasePart") then
					local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
					tween2:Play()
					wait(0.5)
					local tween = tweenservice:Create(v, info, {Color = selColor})
					tween:Play()
				end
			end)
		end
		for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
			task.spawn(function()
				if v:IsA("UnionOperation") then
					v.UsePartColor = true
					local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
					tween2:Play()
					wait(0.5)
					local tween = tweenservice:Create(v, info, {Color = selColor})
					tween:Play()
				end
			end)
		end
	end
end)
local array = {} -- Add all of the parts; i.e. {game.Workspace.ChangingParts.Part1, game.Workspace.ChangingParts.Part2, etc...}

for i,v in ipairs(array) do
    task.spawn(function()
        -- Tweening code here.
    end)
end

Thank you for the help! Really appreciate it.

Also, in addition to what I just said, you should probably do everything within one loop.

local tweenservice = game:GetService("TweenService")

local info = TweenInfo.new(0.5, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut, 0, false, 0)

game.ReplicatedStorage.ColorChangerEvents.Change.OnServerEvent:Connect(function(plr, r, g, b, stafflock, groupid, minRank)
	print(r, g, b)
	local selColor = Color3.fromRGB(r, g, b)
	print(selColor)
	if stafflock == true then
		if plr:GetRankInGroup(groupid) >= minRank then
			for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
				task.spawn(function()
					if v:IsA("BasePart") then
						local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
						tween2:Play()
						wait(0.5)
						local tween = tweenservice:Create(v, info, {Color = selColor})
						tween:Play()
					elseif v:IsA("UnionOperation") then
						v.UsePartColor = true
						local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
						tween2:Play()
						wait(0.5)
						local tween = tweenservice:Create(v, info, {Color = selColor})
						tween:Play()
					end
				end)
			end
		end
	else
		for i,v in pairs(game.Workspace.ChangingParts:GetDescendants()) do
			task.spawn(function()
				if v:IsA("BasePart") then
					local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
					tween2:Play()
					wait(0.5)
					local tween = tweenservice:Create(v, info, {Color = selColor})
					tween:Play()
				elseif v:IsA("UnionOperation") then
					v.UsePartColor = true
					local tween2 = tweenservice:Create(v, info, {Color = Color3.fromRGB(0, 0, 0)})
					tween2:Play()
					wait(0.5)
					local tween = tweenservice:Create(v, info, {Color = selColor})
					tween:Play()
				end
			end)
		end
	end
end)