Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
3

How to check through multiple BoolValues?

Asked by 5 years ago

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.

1function CheckValues()
2 for i,v in pairs (ValuesFolder:GetChildren()) do
3  if v:IsA('BoolValue') and v.Value then
4      print("All true")   
5  end
6 end
7end

2 answers

Log in to vote
3
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago
01local Bools = ValuesFolder:GetChildren()
02 
03local function CheckForAllTrue(Bools)
04    for _,Boolean in pairs(Bools) do
05        if (Boolean:IsA("BoolValue") and Boolean.Value ~= true) then
06            return false
07        end
08    end
09    return true
10end
11 
12if (CheckForAllTrue(Bools)) then
13    print("All true")
14end

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

0
U copied my answer.... although this is a simpler solution R_LabradorRetriever 198 — 5y
0
I prefer this answer, because in most cases it will be faster than going through every boolvalue every time. gskw 1046 — 5y
0
true R_LabradorRetriever 198 — 5y
0
:D R_LabradorRetriever 198 — 5y
Ad
Log in to vote
2
Answered by 5 years ago
Edited 5 years ago

Use a table and a variable:

01local values  = ValuesFolder:GetChildren() -- table
02 
03function areTheyAllTrue(things)
04local var = true -- local variable to be returned
05 for i,v in pairs (things) do
06  if v:IsA('BoolValue') and v.Value then
07        var = true
08      else
09        var = false
10    break -- if even one is false, its set to false and breaks so it returns false immediatlely
11  end
12    return var -- returns the final value
13 end
14end
15 
16if  areTheyAllTrue(values) then
17    print("All values true") -- if its all true, print "All values true"
18end

Answer this question