Counting BoolValues using in pairs

I am making a Pet Index for my game and making a TextLabel that will show out of 100 how many pets the player has found.

I’ve been using the in pairs function to find all the BoolValues that are true and that has worked perfectly fine.

Now my script has to count how many of the BoolVales are set to true. Nothing so far hasn’t worked for me and I would like any tips to accomplish this.

1 Like

You would keep a variable outside of the for loop; this will be the variable which will tell you how many BoolValues are set to true, so each time you iterate through a true BoolValue, you would just up the count by 1.

So in action it would look something like this

local amount = 0

for i,v in ipairs(boolvaluesholder:GetChildren()) do
   if v.Value then
      amount = amount + 1
   end
end

print(amount)

I did that and it worked, but what function should I used besides a while wait because it will keep adding and I don’t want that to happen it would say 12810/100 pets found

You would :Connect() to a .ChildAdded event and put what I posted before into that.

boolvaluesholder.ChildAdded:Connect(function()

    local amount = 0

    for i,v in ipairs(boolvaluesholder:GetChildren()) do
        if v.Value then
         amount = amount + 1
       end
    end

    print(amount)

end)
2 Likes