How to only print after a loop is done

I need help with the following loop:

local totalpop = 0
for _, child in pairs(workspace:GetDescendants()) do
	
	if child:IsA("BasePart") and child.Name == "building" and child.Locked == false then
		local people = ((child.Size.Y*child.Size.X*child.Size.Z)/0.006)*50
		totalpop = totalpop+people
		
	wait(0.001)
		print(totalpop)
	end
end

The loop works as intended, however, I’m wondering if it’s possible to only print totalpop after the loop is done, because as of now the script spams my output like this:

Screenshot 2021-10-16 202001

If this is possible, can someone tell me how it is done? Thank you in advance.

Simply move the print outside the loops block (signified by the “end” keyword) :wink: I also don’t see the reason for the wait() call.

local totalpop = 0
for _, child in pairs(workspace:GetDescendants()) do
	if child:IsA("BasePart") and child.Name == "building" and child.Locked == false then
		
        local people = ((child.Size.Y * child.Size.X * child.Size.Z) / 0.006) * 50
		totalpop = totalpop + people
	end
end
print(totalpop)

This worked, thanks. Can’t believe the solution was that simple lol

Also, I added the wait because without it the output spam would cause Studio to stop responding for a second. But that’s not an issue anymore

1 Like