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

Tweenservice is better and more efficient. To use it, use its functions EXAMPLE:

TweenService:Create(RedBrick,TweenInfo,{Color3 = Color3.fromRGB(0,0,255)}):Play()
1 Like

better and more efficient doesnā€™t really help when I dont understand it though.

1 Like

What do you not understand? Iā€™ve explained it. (I will not respond until tomorrow, so you will have to ask someone else for help now. It is my curfew.)

1 Like

Youā€™ve explained what tweens do, but not how they can be utilised.

If you can use tweenService, it is generally a very good tool to use.

Iā€™ll try to give a fairly easy example relating to what you want. For this example we will be turning a block red over time

As for how it works you need to create a tween. And we will grab a part from workspace as an example

Firstly TweenService gets loaded

local TweenService = game:GetService("TweenService")
local part = game.Workspace.Part

Now our end goal is to call :Play() on a tween object to get it to run.

local TweenService = game:GetService("TweenService")
local part = game.Workspace.Part

local tween = nil --We need this to become a tweenObject so that the :Play method doesn't error.
tween:Play() --This is your goal

Of course right now the code above would error because we havenā€™t created a tweenObject. To do that we need to call TweenService:Create().

It takes 3 things as parameters

  1. The instance you want to tween. In our case Part.
  2. The tweenInfo for tweening. This will be the last thing we do so donā€™t worry about this too much yet. Just know it goes here.
  3. The data you wish to tween. In this case we want to change the color. Tweens will always go from what the instance currently has set as the value for the properties here to their goal. So if the part was white it would go from white to red. If it started as black it would go from black to red. Keep in mind you can tween multiple things at once like this. It takes a dictionary as the value with the key equal to the name of the property you are changing and the value the goal.
local TweenService = game:GetService("TweenService")
local part = game.Workspace.Part

local tweenInfo = nil --We will set this up later

local tween = TweenService:Create(part, tweenInfo, {Color = Color3.new(1,0,0)}) --We need this to become a tweenObject so that the :Play method doesn't error.

tween:Play() --This is your goal

Alright. Now we are almost there. We just need to go back and setup that TweenInfo. Iā€™m just going to leave everything other than the assignment out this time because itā€™s getting big.

local tweenInfo = TweenInfo.new(2)

That is the minimum really needed for a tweenInfo. Just stating the time. The rest of the data will be filled in with their defaults automatically.

What all of the options of TweenInfo do
  1. Time in seconds of how long the tween takes
  2. EasingStyle. This is how the speed of the tween changes over time. A good animated example of this being used in tweening positions of UI elements is in this link. EasingStyle
  3. EasingDirection. Most of the EasingStyles are not symmetrical. This describes which direction it goes. Itā€™s also animated in the link supplied for the last one.
  4. How many times the tween should repeat after tweening once
  5. A boolean that if true says the tween should play in reverse after finishing
  6. How long to wait after saying:Play() to run the tween. I donā€™t usually use repeat so Iā€™m unsure if this also affects length between repeats.

An example of tweenInfo completely filled out

TweenInfo.new(2, Enum.EasingStyle.Quad, Enum.EasingDirection.In, 0, true, 2)

That will create a tween that lasts for 2 seconds. Has itā€™s speed as the quad type. Itā€™s direction as in. It wonā€™t repeat. It does reverse, and it will take 2 seconds after :Play() to run.

Now we have a script that creates and runs a tween

local TweenService = game:GetService("TweenService")
local part = game.Workspace.Part

local tweenInfo = TweenInfo.new(2)

local tween = TweenService:Create(part, tweenInfo, {Color = Color3.new(1,0,0)}) --We need this to become a tweenObject so that the :Play method doesn't error.

tween:Play() --This is your goal

The code doesnā€™t yield after you call :Play(). Once you call play it makes it so something in the background is handling all of the data for you so your script can just continue without worrying about it. If you need to wait for a tween to end there is an event for that but thatā€™s beyond the scope of this.

==================================================================

Now that tweens have been covered, time to focus more on your specific problem.

You want it to when you call a function change the color of lights over time.

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

TweenInfo is not going to change for every tween so you can just set it up at the beginning of the function. Since tweens donā€™t yield it will go through the whole function without waiting so all the tweens will have started at practically the same time.

local TweenService = game:GetService("TweenService")
local function LightsOn(WindowName, R, G, B)
    local tweenInfo = TweenInfo.new(2)
	local TrainParts = script.Parent:GetDescendants() --This creates an array of all parts within the main train model.
    for_, C in pairs(TrainParts) do
        if C.Name == WindowName then
            local tween = TweenService:Create(C, tweenInfo, {Color = Color3.fromRGB(R,G,B)})
            tween:Play()
        end
    end
end

If you were to call this function again in the middle of its tween it would just override it so you donā€™t have to worry about waiting for it to finish before calling it again.

Wow I did not realize my post got this bigā€¦ Hopefully it makes sense.

1 Like

Thanks so much for taking the time to give a detailed explanation on how tween service is used. This certainly does help a little bit as to understanding how tweens work, and once Iā€™m not tired Iā€™ll be sure to properly test around with tweens. Thanks again!

1 Like

I have. Either you skipped some of my posts or youā€™ve ignored them.