For loop running multiable times

Hi, ok so i already made a post about this but its a day old sooo… ok bassicly i know the problem but i just can seem to fix it, bassicly its running the amount of times that something exists, so like lets say (i am using CollectionService aka tagging) so there is like 8 windows the for loop would run 8 times for one window heres the script:

local Lighting = game.Lighting

local CS = game:GetService("CollectionService")

local A = false

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)
		Su2.LightInfluence = 1
		Image2.BackgroundColor3 = Color3.new(0.996078, 1, 0.941176)
		
	elseif Va == 2 then
		Su2.LightInfluence = 0
		
		Image2.BackgroundColor3 = Color3.new(1, 1, 0.709804)
		Su.LightInfluence = 1
		Image.BackgroundColor3 = Color3.new(0.996078, 1, 0.941176)
		
	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
		
		if A == true then
			for i, v in CS:GetTagged("WindowLight") do
				WindowLight(v)

				task.wait(5)
				A = false
			end
		end

	elseif Lighting.ClockTime >= 7 or Lighting.ClockTime <= 18.5 then
		for i, v in pairs(CS:GetTagged("WindowLight")) do
			WindowTurnOffLight(v)
		end
		A = true
	end
end)
2 Likes

Well, of course. That’s exactly how you’ve programmed it to work. What is it you’re trying to achieve exactly?

3 Likes

Well i am trying for the for script to only run 1 time for each window so the script would run for window 1 and window 2 and window 3 ect only once intill the next night

2 Likes

I’m not sure I understand. Can you rephrase this?

3 Likes

oh well i want it to only run once for each window not 8 times for one window

2 Likes

I believe you mean to use and in your if-statements, not or. This also enables you to simplify your elseif clause to an else caluse

3 Likes

wait i dont know if you really understood but i ment like lets say there is one window right and i want that window to have a random chance to light up or not, right, So i make the function to give a random number to Va to make the window light up or not, But the problem is the script runs like 8 times for that one window because it runs for every 1 window, aka the function starts 8 times on 1 window

1 Like

The function will run for everything tagged “WindowLight”, and nothing more. If you’re seeing the function run multiple times for one “WindowLight” tagged instance, it’s due to your logic. I recommend changing your ors to ands, as the current code does not properly restrict the activation and deactivation for windows based on time frames

1 Like

Uh well i tried it but this time it actually broke the hole thing and now none of them are turning on

1 Like

Hi @iuseselfnone

Problem is this:

	elseif Lighting.ClockTime >= 7 or Lighting.ClockTime <= 18.5 then
		for i, v in pairs(CS:GetTagged("WindowLight")) do
			WindowTurnOffLight(v)
		end
        A = true
	end

Maybe this will work.
Its not the best solution but its the simplest i can think of:

Edit Source has been modified because of some mistakes.

local Lighting = game.Lighting

local CS = game:GetService("CollectionService")

local A = true
local B = false

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)
		Su2.LightInfluence = 1
		Image2.BackgroundColor3 = Color3.new(0.996078, 1, 0.941176)

	elseif Va == 2 then
		Su2.LightInfluence = 0

		Image2.BackgroundColor3 = Color3.new(1, 1, 0.709804)
		Su.LightInfluence = 1
		Image.BackgroundColor3 = Color3.new(0.996078, 1, 0.941176)

	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

		if A then
			
			A = false
			
			for i, v in CS:GetTagged("WindowLight") do
				WindowLight(v)

				task.wait(5)
			end
			
			B = true
		end

	elseif Lighting.ClockTime >= 7 or Lighting.ClockTime <= 18.5 then
		
		if B then
			B = false
			
			for i, v in pairs(CS:GetTagged("WindowLight")) do
				WindowTurnOffLight(v)
			end
			
			A = true
		end
	end
end)

You can modify A and B depending on world start ClockTime.
If you didn’t get result you wanted ,just make

local A = false
local B = true
1 Like

Honestly, I think we should try for a re-write. Can you explain what visual effect your code was meant to achieve?