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.
1 | function 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 |
7 | end |
01 | local Bools = ValuesFolder:GetChildren() |
02 |
03 | local 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 |
10 | end |
11 |
12 | if (CheckForAllTrue(Bools)) then |
13 | print ( "All true" ) |
14 | end |
Use a table and a variable:
01 | local values = ValuesFolder:GetChildren() -- table |
02 |
03 | function areTheyAllTrue(things) |
04 | local 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 |
14 | end |
15 |
16 | if areTheyAllTrue(values) then |
17 | print ( "All values true" ) -- if its all true, print "All values true" |
18 | end |