Why is for i not working well

Hi, i am trying to make a system where windows turn on like if light turned on, And it works really really well, Execpt it sucks, Yup, It does it really well but it just keeps going and going like one light turns on then the other then the other then the other ect, intill ALL of them turn on (which clearly is not the thing i want) heres the script:

local Lighting = game.Lighting

local CS = game:GetService("CollectionService")

local function WindowLight(Window)
	local Va = 1
	
	local Su = Window.SurfaceGui
	local Su2 = Window.SurfaceGui2
	
	local Image = Su.ImageLabel
	local Image2 = Su2.ImageLabel

	Va = math.random(1, 5)

	
	if Va == 4 then
		Su.LightInfluence = 0
		
		Image.BackgroundColor3 = Color3.new(1, 1, 0.709804)
		
	elseif Va == 2 then
		Su2.LightInfluence = 0
		
		Image2.BackgroundColor3 = Color3.new(1, 1, 0.709804)
		
	end
end

local function WindowTurnOffLight(Window)
	local Su = Window.SurfaceGui
	local Su2 = Window.SurfaceGui2
	
	local Image = Su.ImageLabel
	local Image2 = Su2.ImageLabel
	
	Su.LightInfluence = 1
	Su2.LightInfluence = 1
	
	Image.BackgroundColor3 = Color3.new(0.996078, 1, 0.941176)
	Image2.BackgroundColor3 = Color3.new(0.996078, 1, 0.941176)

end

Lighting:GetPropertyChangedSignal("ClockTime"):Connect(function()
	if Lighting.ClockTime>= 18.5 or Lighting.ClockTime <= 7 then
		for i, v in pairs(CS:GetTagged("WindowLight")) do
			WindowLight(v)
			wait(0.5)
		end
	elseif Lighting.ClockTime >= 7 or Lighting.ClockTime <= 18.5 then
		for i, v in pairs(CS:GetTagged("WindowLight")) do
			WindowTurnOffLight(v)
		end
	end
end)

so how could i fix tihs?

1 Like

I am sure it is the wait(0.5) below WindowLight(v) what is causing the delay between each window light (because it yields the script for 0.5 seconds after each light gets turned on), try making the putting:

wait() – this should make them light up all at once! EDIT: By this i mean just remove the 0.5 in the brackets on wait() to remove the delay between each window turning on

2 Likes

Thank you! Sorry i forgot to put a video because i am trying to the opposite i want only a few to light up but they all light up

You see how they all light up i want it to be only a few but they all light up

1 Like

You could try

The below should make it run a certain amount of times although a flaw is that it can try to turn the same window on multiple times

local WindowTable = CS:GetTagged("WindowLight")
local AmountToLight = math.random(1,#WindowTable)
for i=1, AmountToLight do
task.wait(0.5)
local Window = WindowTable[1,#WindowTable]
WindowLight(Window)
end

this is not very optimised although so i would not recommend it but it should hopefully work

You can do this

for i, v in CS:GetTagged("WindowLight") do
    if math.random(1, 3) == 1 then -- 1 chance out of 3 to be enabled
        WindowLight(v)
    end
end

If you want a few to light up, make it so that more lights dim than light up.

for i,v in pairs(CS:GetTagged("WindowsLight")) do
    if i % 3 == 0 then
         task.wait(.5)
         WindowLight(v)
    end
end
for i,v in pairs(CS:GetTagged("WindowsLight")) do
         task.wait(.3)
         WindowTurnOffLight(v)
    end
end

Also minor suggestion, there’s a logic error at these lines of code. To fix this, replace the or’s with and’s like this, so that the lights will turn on/off at the correct times:

	if Lighting.ClockTime>= 18.5 and Lighting.ClockTime <= 7 then

	elseif Lighting.ClockTime >= 7 and Lighting.ClockTime <= 18.5 then