Is there anyway to check if a certain amount of values are TRUE?

so basically i was tryna think of a way so that i could run something once certain amount of values are true in a folder. For example i have 8 values and currently the code runs once all 8 values == true but i want it to run once any 6 of them == true is it possible to do that?

1 Like

To check if any six are true you could assign a variable to 0, then loop through each value in the folder, incrementing the variable if the value is true. Then, simply check if the value is greater than or equal to six.

1 Like

thats actually a pretty smart way i didnt think of. thanks a lot!

local folder = game.ReplicatedStorage:WaitForChild('Folder')
local numtrue = 0

local function CodeToRun()
	print("Code to run here!")
end

local function CheckNumber(item, NumberRequired)
	for _, v in next, item:GetDescendants() do
		if v:IsA("BoolValue") and v.Value == true then 
			numtrue += 1
		end
		if numtrue >= NumberRequired then
			return true
		end
		if _ == #item:GetDescendants() then
			numtrue = 0
			return false
		end
	end
end

if CheckNumber(folder, 5) == true then
	CodeToRun()
else
	print("Number not met!")
end

Just a super rough and quick example but it should be a good base for what you need. Optimizing that is on you. Hope this helps!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.