How to create a for loop that gradually modifies the colour property of multiple parts simultaneously

  1. What do you want to achieve?
    I am trying to create a for loop that gradually increases the colour property of multiple parts from an array created by :GetDescendants()
    I am wanting to have each part modified at the same time, so that if I implement a wait() the wait affects the loop speed rather than the delay between each part modification

  2. What is the issue?
    I dont know how I would use a for loop in such a way as to modify multiple parts at the same time.

  3. What solutions have you tried so far?
    I’ve looked for tutorials on youtube and searched generally on the web for any help but to no avail. DevForums is the next solution to my problem, hopefully!

My code so far to help:

local function LightsOn(WindowName, R, G, B)
	local TrainParts = script.Parent:GetDescendants() --This creates an array of all parts within the main train model.

	for _, C in ipairs(TrainParts) do
		if C.Name == WindowName then
			C.Color = Color3.fromRGB(R,G,B)
		end
	end
end

WindowName is the name of the part(s) that should be modified
R, G and B are 3 separate int values that let the script know the desired RGB colour to set the parts to, however I would like to fade these in gradually via the loop however I’m not sure how to do that with this loop that filters out the correct parts, with a for loop with modifiable numbers I could do that, however I am a bit clueless as to what to do here!

I am looking for help in creating a for loop that does this, and not a tween as I unfortunately dont understand tweenservice beyond the slightest.

1 Like

You just need to put the wait outside of the for loop.

while true do
    task.wait()
    for _, part in pairs(example:GetDescendants()) do
        --do things
    end
end
2 Likes

Please do something like this: (this script assumes you have already defined the variables)

local parts = group:GetDescendants()

local function LightsOn(WindowName, R, G, B)
    for _, part in pairs(parts) do
        TweenService:Create(part,tweeninfo,{Color3 = Colour3.fromRGB(R,G,B)}):Play()
    end
end
2 Likes

I see, however how would I manage to do this with my 3 separate R, G and B values? I presume that I would plug those into the colour value as R, G, B and that would work?

2 Likes

I posted that before you edited your post, I will modify it RQ.

2 Likes

yeah sorry should’ve included the code before posting lol, thanks

I dont have a lot of experience with TweenService, how would I speed up or slow down the tween speed?

1 Like

Use coroutines. They allow you asyncronously execute code.

local function LightsOn(WindowName, R, G, B)
	local TrainParts = script.Parent:GetDescendants() --This creates an array of all parts within the main train model.

	for _, C in ipairs(TrainParts) do
                coroutine.resume(coroutine.create(function()
                       if C.Name == WindowName then
			     C.Color = Color3.fromRGB(R,G,B)
		      end
               end))
	end
end

EDIT: Sorry for the formatting, your formatting is weird so it messed mine up

EDIT2: Also use tweens inside the coroutine

1 Like

Well, first define the variable by using game:GetService("TweenService"), then make a new tweeninfo. For more info, please use TweenInfo and TweenService

1 Like

I have no clue if this is what you are looking for?

function ColorGradual(R, G, B)
     local Parts = script.Parent:GetDescendants()

     for Count = 1, 100 do
          wait(0.01)
          
          for index, value in pairs(Parts) do
               value.Color = Color3.fromRGB(R+Count, G+Count, B+Count)
          end
     end
end
1 Like

the R, G and B values are the variables that control the values for Red, Green and Blue for modifying the Colour3 property. I am looking to fade the values up from 0 to the values set by the R, G and B variables. This would just add 100 to the values equally.
For example, 255,255,0 would be set as:

R = 255
G = 255
B = 0

What I am looking to achieve is to fade these values up, and it should be noted that the values obviously wont be as simple as these, so I would need to divide the values and then multiply them by the count during the loop, however I cant quite think of how the logic would work for that

1 Like

Yeah those sources dont seem to explain how the properties work in practice very well unlike other API pages, I dont really get them right now lol. Might have a test around with the functions later

1 Like

What do you not understand, exactly?

1 Like

I dont understand how I would use the TweenInfo functions to modify the tween. There’s no example code unlike other API sources. I’m more familiar with for loops and previous attempts to understand the tweenservice haven’t gone very well lol, just leaves me scratching my head

1 Like

TweenInfo is used for:

Tween time/length
Easing style
Easing direction
The delay
If it repeats
How long it repeats

1 Like

I get what its used for, I dont get how it would be used to do such

1 Like

Such as what? You mean how it would be used for the colour?

1 Like

I dont really get how any of it works tbh
I’ve had a read through the API references you sent me but I dont really seem to grasp it

1 Like

Tweening is used for transitions. For example, if you wanted to change a red brick’s colour from red to blue and you wanted it to smoothly transition instead of just happening, you’d tween it.

1 Like

yeah I understand what tweening is used for but I dont get how its used to make that happen
I’ve always used for loops to gradually change a colour property

1 Like