How to check when all v's are true?

script.Parent.MouseClick:Connect(function(plr)
	local estats = plr.extrastats
	if estats then
		for i,v in pairs(estats:GetChildren()) do
			if v.Value == true then
				print(v.Name.." is true")
			end
		end
		
		print("badge award is yes")
	end
end)

This is a server script in a click detector.

Whenever I click it even with only 3/5 or something that isn’t 5/5 eggs it still says badge award is yes, but if I put the print inside of the loop it says badge award is yes for every egg it checks. How would I make it so when it checks and finds all of the eggs are true, it returns the print “badge award is yes”?

It still prints that because the loop ended and the code continued

1 Like

But it prints as the loop starts, not as it ends.

Loop and return on the first instance that is false.

local values = {true, false, true true}

function somn()
   for _,v in ipairs (values) do -- ignore the ipairs as it's for arrays
      if v==false then return end -- this will make the function end
   end
   print "We made it!" -- will only print if the function didn't return
end
3 Likes

Instead of that try this.

local EggsCollected = 0

script.Parent.MouseClick:Connect(function(plr)
	local estats = plr.extrastats
	if estats then
		for i,v in pairs(estats:GetChildren()) do
			if v.Value == true then
				EggsCollected = EggsCollected + 1
			end
		end
		if EggsCollected == 5 then
           print("Give Badge")
        else
           EggsCollected = 0 --Resets the Counter
        end
	end
end)
2 Likes

Try this:

local t = {}

for i,v in pairs(estats:GetChildren()) do
	if v.Value == true then
        table.insert(t, tostring(true)) -- inserts a 'true' value into the table
		print(v.Name.." is true")
    else
        table.insert(t, tostring(false)) -- inserts a 'false' value into the table
	end
end
		
if not table.find(t, 'false') then -- check if everything is true
   print("badge award is yes")
end

or what @benpinpop did

1 Like

Most of these solutions are overly-complicated.

script.Parent.MouseClick:Connect(function(plr)
	local estats = plr.extrastats
	if estats then
		local allTrue = true
		for _, v in ipairs(estats:GetChildren()) do
			if (not v.Value) then
				allTrue = false
				break
			end
		end
		print(allTrue and "All are true" or "Some are false")
	end
end)
2 Likes