Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to check through multiple BoolValues?

I want to go through a folder full of BoolValues and check if they are all set to true, if so then something will happen, if not then nothing will happen.

The code below checks through the folder, but doesn't do what I need. It just checks them individually and will execute the code even if only one BoolValue is set to true.

I hope this makes sense.

function CheckValues()
 for i,v in pairs (ValuesFolder:GetChildren()) do
  if v:IsA('BoolValue') and v.Value then 
	  print("All true")    
  end
 end
end

2 answers

✓ Accepted answer
local Bools = ValuesFolder:GetChildren()

local function CheckForAllTrue(Bools)
	for _,Boolean in pairs(Bools) do
		if (Boolean:IsA("BoolValue") and Boolean.Value ~= true) then
			return false
		end
	end
	return true
end

if (CheckForAllTrue(Bools)) then
	print("All true")
end

Efficient adjustment to @R_LabradorRetriever's answer > original layout credit.

4 comments

0

U copied my answer.... although this is a simpler solution

0

I prefer this answer, because in most cases it will be faster than going through every boolvalue every time.

0

true

0

:D

Use a table and a variable:

local values  = ValuesFolder:GetChildren() -- table

function areTheyAllTrue(things)
local var = true -- local variable to be returned
 for i,v in pairs (things) do
  if v:IsA('BoolValue') and v.Value then 
      	var = true
      else
      	var = false
	break -- if even one is false, its set to false and breaks so it returns false immediatlely
  end
	return var -- returns the final value
 end
end

if  areTheyAllTrue(values) then
	print("All values true") -- if its all true, print "All values true"
end

This discussion is preserved as it was. New answers, comments, and votes are closed.