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 4 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.

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

Log in to vote
3
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago
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.

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

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

Answer this question