So, I have a table full of variables that are defined by a datastore. I need to cycle through all the variables and check if they are true. Lets say that 4 of the variables are true, then the script would set the variable Count to 4, or if 7 of the variables are true Count would equal 7. How would I do this?
We can use a pairs iterator to enumerate through a table. Evaluate for a True attribute, and increment a count. If we embody this within a function, we can supply any array and retrieve a result:
01 | local ArbitraryTable = { true , false , true , true , false } |
02 |
03 | local function GetTrueAttributes(Table) |
04 | local TrueBools = 0 |
05 | for _,Boolean in pairs (Table) do |
06 | if (Boolean = = true ) then |
07 | TrueBools = (TrueBools + 1 ) |
08 | end |
09 | end |
10 | return TrueBools |
11 | end |
12 |
13 | local TotalTrue = GetTrueAttributes(ArbitraryTable) |
14 | print (TotalTrue) --// 3 |
If this helped, don’t forget to accept this answer!