Trouble with a for loop

I am having trouble exiting this for loop it calls the for loop and then It checks the first child, and then it finds the one with that name, but it never exits the loop, and sometimes it wont even go in the for loop

for i, v in ipairs(Buildings:GetChildren()) do
print(“In For”)
if v.Name == “ClassicBuilding” then
print(“In Classic”)
v.Material = Enum.Material.Neon
elseif v.Name == “CircleBuilding” then
print(“In Circle”)
v.Material = Enum.Material.Neon
elseif v.Name == “TowerBuilding” then
print(“In Tower”)
v.Material = Enum.Material.Neon
elseif v.Name == “TwoPartBuilding” then
print(“In Two”)
v.Material = Enum.Material.Neon
else
print(v.Name)
end
print(“Bottom For”)

here is the whole script

local Time = 1
local TimeChange = 0.01

local bankWindows = game.Workspace.Buildings.Completed.Bank.Windows
local connectedWindows = game.Workspace.Buildings.Completed.ConnectedBuilding.Windows

local Buildings = game.Workspace.Buildings.Completed

while wait(time) do
game.Lighting.ClockTime = game.Lighting.ClockTime + TimeChange

if game.Lighting.ClockTime > 18 or game.Lighting.ClockTime < 6 then
	bankWindows.Material = Enum.Material.Neon
	
	for i, v in ipairs(connectedWindows:GetChildren()) do
		v.Material = Enum.Material.Neon
	end
	
	for i, v in ipairs(Buildings:GetChildren()) do
		print("In For")
		if v.Name == "ClassicBuilding" then
			print("In Classic")
			v.Material = Enum.Material.Neon
		elseif v.Name == "CircleBuilding" then
			print("In Circle")
			v.Material = Enum.Material.Neon
		elseif v.Name == "TowerBuilding" then
			print("In Tower")
			v.Material = Enum.Material.Neon
		elseif v.Name == "TwoPartBuilding" then
			print("In Two")
			v.Material = Enum.Material.Neon
		else
			print(v.Name)
		end
		print("Bottom For")
	end
	print("Out Of For")
else
	bankWindows.Material = Enum.Material.Plastic

	for i, v in ipairs(connectedWindows:GetChildren()) do
		v.Material = Enum.Material.Plastic
	end
	
	for i, v in ipairs(Buildings:GetChildren()) do
		print("In For Else")
		if v.Name == "ClassicBuilding" then
			print("In Classic Else")
			v.Material = Enum.Material.Plastic
		elseif v.Name == "CircleBuilding" then
			print("In Circle Else")
			v.Material = Enum.Material.Plastic
		elseif v.Name == "TowerBuilding" then
			print("In Tower Else")
			v.Material = Enum.Material.Plastic
		elseif v.Name == "TwoPartBuilding" then
			print("In Two Else")
			v.Material = Enum.Material.Plastic
		else
			print(v.Name)
		end
		print("Bottom For Else")
	end
	print("Out Of For Else")
end
print("Out Of If")

end

Not quite sure what you mean never exits the loop. Are you wanting it to stop checking other conditions once it’s found a match? If so use break after you set your material.

well, if it finds a match i want it to run the for loop again through another buildgs:GetChildren() child

is there a better way to write this script so that once its night it will get each object with a certain name and change its material

a table would look much better for those if and elseif statements

you got repeated code

1 Like

For this I’d personally use tags and the CollectionService.

I use this tag editor plugin to tag such parts, we’ll use an example tag “NeonAtNight” for this. I’d probably then have a IntValue stored somewhere in ServerStorage that I’d set to Lighting.ClockTime whenever that’s changed. Then do something like:

local CollectionService = game:GetService("CollectionService")
local TimeVar = game.ServerStorage.CurrentTime

TimeVar.Changed:Connect(function()
    if TimeVar.Value == 18 then
        for _, v in pairs(CollectionService:GetTagged("NeonAtNight")) do
            v.Material = Enum.Material.Neon
        end
    end

    if TimeVar.Value == 6 then
        --Do the same here but for whatever material you want in day
    end
end)
1 Like