Fading multiple lights at once

Hi! I have been trying to make a script that would grab all lights and fade them out, then change the color, and then fade back in. The issue with the way I am doing it is, they all go one-by-one. I have no idea how to fix this as using this method is the only way I have done in previous ways, so usually I avoid trying to fade anything involving lighting, but I decided I may as well learn something new today.

Any help is appreciated!

Script
local MoodLightingFolder = game.Workspace.PlaneModel.CTLighting


game.ReplicatedStorage.CasualTechMoodLighting.SetLighting.OnServerEvent:Connect(function(plr,ColorName,Color)
	
	
	for _,LightPart in pairs(MoodLightingFolder:GetChildren()) do
		LightPart.SurfaceLight.Brightness = 0.7
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.6
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.5
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.4
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.3
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.2
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.1
		wait(0.1)
		LightPart.SurfaceLight.Color = Color
		LightPart.SurfaceLight.Brightness = 0.2
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.3
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.4
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.5
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.6
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.7
		wait(0.1)
		LightPart.SurfaceLight.Brightness = 0.8
		wait(0.1)		
	end
	
end)
1 Like

You can fix this issue by utilizing coroutines or spawn functions.

Example:

local MoodLightingFolder = game.Workspace.PlaneModel.CTLighting


game.ReplicatedStorage.CasualTechMoodLighting.SetLighting.OnServerEvent:Connect(function(plr,ColorName,Color)
	for _,LightPart in pairs(MoodLightingFolder:GetChildren()) do
		spawn(function()
			LightPart.SurfaceLight.Brightness = 0.7
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.6
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.5
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.4
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.3
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.2
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.1
			wait(0.1)
			LightPart.SurfaceLight.Color = Color
			LightPart.SurfaceLight.Brightness = 0.2
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.3
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.4
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.5
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.6
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.7
			wait(0.1)
			LightPart.SurfaceLight.Brightness = 0.8
			wait(0.1)		
		end)
	end
end)
3 Likes

Have you tried using a loop within a loop…?

for level = 100, 0, -1 do
    for _, light in ipairs(lights) do
        light.Brightness = level/100
    end
    wait()
end
for _, light in ipairs(lights) do
    light.Color = myColor
end
for level = 0, 100 do
    for _, light in ipairs(lights) do
        light.Brightness = level/100
    end
    wait()
end
4 Likes

I would suggest using TweenService instead of copying the same line over and over and using wait(). It will make the transition much smoother and can use only one line of code! Here is the documentation for TweenService:Create(): TweenService | Documentation - Roblox Creator Hub

2 Likes

Of course! Tween service implementation here would be great. I was simply fixing the part of his code that he asked to be fixed but improvements are always welcome.

2 Likes

As much as I love all of your replies, and I want to learn many new things, I mainly understand the spawn function mostly, as it seems to be the smallest :thinking:

@Fire540Games
I have always loved using TweenService, but I am used to using it only on UI, haha.
I believe I understand how it would work with the lights, so I may as well give it a try :eyes:

1 Like

Implementing Tween Service may actually be your better bet here although I love coroutines. Try the following:

local object = LightPart.SurfaceLight;
local tweenInfo = TweenInfo.new(0.1);
local changeVal = 0; -- Change to whatever you need it to be at the moment (i.e. 0.8 for highest brightness and 0 for lowest

game:GetService("TweenService"):Create(object, tweenInfo, {Brightness = changeVal}):Play();

Note: you don’t need to redefine those variables. I just added them to help you.

2 Likes