The script is doing the parts one by one. It first picks a part. Then it lerps its color, waits, lerps, waits etc…
It does not start doing this for the other parts until it’s done.
Here’s what you get when rearrange the first 3 lines of the loop.
local parts = script.Parent:GetChildren()
while true do -- do the following repeatedly forever
for i, v in pairs(parts) do -- pick a part
if v:IsA("BasePart") then
for i = 0,2,0.05 do
-- make that one chosen part change color
v.Color =v.Color:lerp(Color3.new(255/255, 18/255, 225/255), i / 100)
wait()
end
for i = 0,2,0.05 do
v.Color =v.Color:lerp(Color3.new(30/255, 41/ 255, 255/255), i / 100)
wait()
end
-- etc etc...
end
end
end
Now the script will, one by one, make each of the parts go through the spectrum.
This seems like an improvement, because before, it was stuck on just one of the parts!
Here’s how you’d rearrange the script to work how you expected it to:
while true do -- do the following repeatedly forever
for i = 0,2,0.05 do
for i, v in pairs(parts) do -- pick a part
if v:IsA("BasePart") then
v.Color =v.Color:lerp(Color3.new(255/255, 18/255, 225/255), i / 100) -- set its color
end
end
wait() -- we've set the colors of all parts, let's finally wait
end
for i = 0,2,0.05 do
for i, v in pairs(parts) do
if v:IsA("BasePart") then
v.Color =v.Color:lerp(Color3.new(30/255, 41/ 255, 255/255), i / 100)
end
end
wait()
end
-- etc. etc...
end
But there’s a simple way to turn your first code into what you expected, and that’s making a new thread/coroutine for each part.
local parts = script.Parent:GetChildren()
local function makePartCycleColors(part)
while true do
for i = 0,2,0.05 do
v.Color =v.Color:lerp(Color3.new(255/255, 18/255, 225/255), i / 100)
wait()
end
-- etc etc etc. copy-paste your own example here
end
end
for i, v in pairs(parts) do
if v:IsA("BasePart") then
-- wrap the above function in a coroutine
local coro = coroutine.wrap(makePartCycleColors) -- coro is actually a function
coro(v) -- call the function with the part
-- when it uses wait() for the first time, it returns right here, to
-- continue the loop and make a coroutine for the next part
-- the game will then resume the coroutine after that wait() has finished,
-- independently from this loop and anything else happening
-- what if you didn't use coroutine.wrap()? what if you just used
-- makePartCycleColors(v)?
-- then wait() would pause this for loop, and the for loop wouldn't continue
-- until makePartCycleColors() has finished
-- and that function NEVER finishes, so the next part will never start changing colors
-- it'd be exactly what you had before
end
end