While loop to count children

Hey. I’m making an index for my game and I am making a TextLabel that will say how many out of 180 has been discovered.

What I’ve Tried
I’ve tried using a for loop, it works but it will keep adding so it will make it say something like 1234/180 and that is not what I am trying to do.

I have also tried using a debounce to make it wait like 5 seconds but it just keeps adding after 5 seconds even when the player doesnt have that much discovered.

The reason I tried for loops is because I want it to update as soon as the player discovers one. What I want is the table will count how many BoolValues are true and then print.

1 Like

If you need the amount of children inside a parent you could put a # in front of it. For example:

Local Childs = game.Workspace:GetChildren()
print(#Childs)
2 Likes

I forgot the mention that I want it to count only the BoolValues that are true. Would I add an if statement for this?

1 Like

To count the values you could do:

Local ValuesThatAreTrue = 0
Local Childs = game.Workspace:GetChildren()

for Index, ValueToCheck in pairs(Childs) do
       if ValueToCheck.Value == true then
              ValuesThatAreTrue = ValuesThatAreTrue + 1
       end
end

Label.Text = ValuesThatAreTrue.."/"..#Childs

I am on phone so sorry if somthing is mis spelled.

1 Like

He wants the for loop to be always running

1 Like

Then put the for loop i send inside a while true do with a wait.

1 Like

This will make it so the player will have to reset their character every time for it to update.

1 Like

Edit: Variables where in the wrong place.

While true do
       Local ValuesThatAreTrue = 0
       Local Childs = game.Workspace:GetChildren()

       for Index, ValueToCheck in pairs(Childs) do
              if ValueToCheck.Value == true then
                    ValuesThatAreTrue = ValuesThatAreTrue + 1
              end
       end

       Label.Text = ValuesThatAreTrue.."/"..#Childs

       wait(5)
end
1 Like

You should use remote events instead…

But if u wanted to do it ur way you can do,

local trueValues = 0

boolValue.Changed:Connect(function()
    if boolValue.Value == true then
        trueValue += 1
    end
end)

Sadly you would have to do it for each and single of of them, but you should use events

1 Like

You could put that function into the for loop so you only need one script.

1 Like

boolValue.Changed doesn’t need a while loop it’s already looping

1 Like

Every time a value is “Discovered”, have a counter add +1 to a variable. Wherever you set the boolean to “true” should add a +1 right under it.

Edit: Before ticking the boolean to true, check if it’s false. You could also clamp the variable that is counting upwards like this:

local NumOfTrueBools = math.clamp(NumOfTrueBools, 0, 180) -- Will never go under 0 or past 180

2 Likes

I know but you said you needed to do that for every single one.

im reffering boolValue, bcs theres no for loop, you would have to do each bool