for i, v in pairs(game.Workspace.Model1:GetChildren()) do local intValue = v:FindFirstChild("DataValuel") if v.intValue.Value == 0 then ----do a thing break end end end
As you guys can see, this is an issue because if the code iterates through a brick that does not have an IntValue in it or that has an IntValue but it is not set to 0 then the loop will end, I want to make it so that it will iterate through all of the bricks, and then upon iterating the correct one which is one that has a value of 0 it will do something. Removing the break does what I want, however then it will run more than once, which is what I do not want. Any ideas?
Maybe I create another if statement that checks local variable is ture and at the end of --do a thing I set it to false? I will try that it might work.
You can do this using a simple boolean flag:
local ran; for i, v in pairs(game.Workspace.Model1:GetChildren()) do local intValue = v:FindFirstChild("DataValuel") if intValue.Value == 0 and not ran then ----do a thing ran = true; end --// do other things end end
This will not only check if intValue.Value
is 0, but also if the variable ran
is false or nil. At the end of the code within the if statement, the flag is set to true so it won't run more than once.
for i, v in pairs(game.Workspace.Model1:GetChildren()) do local intValue = v:FindFirstChild("DataValuel") if v.intValue.Value == 0 then -- For examples sake v.BrickColor = BrickColor.new("Bright red") return true end end end
See code above, break should work but I don't know what context your using this code in. If its a function return true should break the loop, and end the function call.