Hello im trying to change colors of parts that have the same name all at the same time using a for loop and tweening but it wont work for some reason here is my code
local tweenservice = game:GetService("TweenService")
local parts = script.Parent.RainbowRoad:GetChildren()
for i, v in pairs(parts) do
local tweeninfocolor = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut,
0, false, 0)
while wait(1) do
tween = tweenservice:Create(parts, tweeninfocolor, {Color = Color3.fromRGB(math.random(1, 255), math.random(1, 255), math.random(1, 255))})
tween:Play()
end
end
Please tell me what i got wrong as i am still a beginner scripter!
1 Like
Put the while loop outside of the for loop. That might work.
edit: or use
spawn(function()
-- put the while loop here
end)
1 Like
I see, you are looping through the parts, but then you do a infinite while loop, so it will only work on the first part it finds because it will never be able to finish the first part’s code. you should have the for i,v in side of the while loop, like this:
local tweenservice = game:GetService("TweenService")
local parts = script.Parent.RainbowRoad:GetChildren()
while wait(1) do
for i, v in pairs(parts) do
local tweeninfocolor = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0)
tween = tweenservice:Create(parts, tweeninfocolor, {Color = Color3.fromRGB(math.random(1, 255), math.random(1, 255), math.random(1, 255))})
tween:Play()
end
end
I can’t test right now but this should work. Let me know!
1 Like
Well now i get a error on line 15 that says “Unable to cast value to Object”.
Try this, you can’t tween a table:
local tweenservice = game:GetService("TweenService")
local parts = script.Parent.RainbowRoad:GetChildren()
while wait(1) do
for i, child in ipairs(parts) do
local tweeninfocolor = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0)
local tween = tweenservice:Create(child, tweeninfocolor, {Color = Color3.fromRGB(math.random(1, 255), math.random(1, 255), math.random(1, 255))})
tween:Play()
end
end
2 Likes
the code I put is only 14 lines, what is on line 15 of your script?
1 Like
I had to remove some varibles that were duplicates now it saves line 10.
wait I see, replace “parts” with v on this line
tween = tweenservice:Create(parts, tweeninfocolor, {Color = Color3.fromRGB(math.random(1, 255), math.random(1, 255), math.random(1, 255))})
the code was using the table “parts” instead of v(the part currently looping on)
1 Like
It works but how can i make them all be showing the same color?
Like still random colors but every part isnt a diffrent color
Save the color outside the for loop and generate it at the top of the while loop, right before the for loop:
local tweenservice = game:GetService("TweenService")
local parts = script.Parent.RainbowRoad:GetChildren()
while wait(1) do
local CurColor = Color3.fromRGB(math.random(1, 255), math.random(1, 255), math.random(1, 255))
for i, child in ipairs(parts) do
local tweeninfocolor = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0)
local tween = tweenservice:Create(child, tweeninfocolor, {Color = CurColor})
tween:Play()
end
end
3 Likes
while wait(1) do
local thiscolor = Color3.fromRGB(math.random(1, 255), math.random(1, 255), math.random(1, 255))--pick a color
for i, v in pairs(parts) do
local tweeninfocolor = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.InOut, 0, false, 0)
tween = tweenservice:Create(v, tweeninfocolor, {Color = thiscolor})--make all parts the picked color
tween:Play()
end
end
1 Like