How do i check if all members of a table meet a criteria

very descriptive title i know

basically i have an in ipairs loop going through an table and im checking if all the parts in the table meet a condition

for i, piece in ipairs(pieces) do
   if piece.Position == randomPart.Position then
      break
   end
end
return(something)

basically i want it to return only if all pieces are not in that position
if it goes through it without any pieces being in that position, it returns
and when the loop detects that there is a part in that position, it breaks the loop because it doesnt have to continue now that it knows a part is in that position
but then because of sequencing it just returns anyways
and i cant have an “else” that returns it because that will return after a single part is found to not be in that position
so

how would i return this ONLY if ALL MEMBERS OF THE TABLE meet the criteria of not being in that position?

why not have a bool that you can set that just checks for that and then you can just return that bool?

local criteria_met = true
for i, piece in ipairs(pieces) do
   if piece.Position == randomPart.Position then
      criteria_met = nil
      break
   end
end
return criteria_met

if i put this in a function, for example, function Criteria()

could i do, for example

local criteria = Criteria()
if criteria == true then
--run code
end

yeah, infact you wouldn’t even need “== true” since everything in lua that isn’t false or nil always ends up being true.

oh thats right

ok that might have just put an end to like an hour of not being able to think or focus and going crazy because i cant come up with an idea on how to fix this which should have been easy

thank you

1 Like