How would I go by checking all values in a table to be set to true?

Dont worry all the other errors that appear in the output, they are intentional.
And I did check if one of the errors are from the script i’m using, but no, none of them are related.


As you can see the print("All Values true") fire’s when only one of the value changes to true, but the idea is that when all of them are true at the same time.

Instead of determining if a value is true, establish if a value is false and storing it as a bool, in a separate variable; doing so enables any presence of a false condition to acknowledge the table is false rather than constantly confirming if a table is true. If that makes sense?

Here is an example, I hope this helps:

local t = {true, false, true, true}

local isTrue = true

for i, v in ipairs(t) do
    if not v then
        isTrue = false
    end
end;

print(isTrue)

--

local t = {true, true, true, true}

local isTrue = true

for i, v in ipairs(t) do
    if not v then
        isTrue = false
    end
end;

print(isTrue)
4 Likes

Make sure to break the loop once you’ve found a false value as well. @Osrock_626 this is the solution you want.

And not for nothing, all the other replies in this thread are completely mind-blowing. Don’t reply if you have no idea what you’re talking about.

1 Like

Ok great, this works as expected.

But now how can I make it re check when any of the values change. For some reason using :GetPropertyChangedSignal() and .Changed don’t fire when the values are being changed.

Script currently using:

for A,B in pairs(script.Values:GetChildren()) do
   B:GetPropertyChangedSignal("Value"):Connect(function() --For some reason, it doesn't work, when it works just fine for other scripts
      local Values = {}
      local Acquired = true --Placed variables here to reset them when function called
      table.insert(Values,A,B.Value) --I dont have to manually insert the values myself
      for A,B in ipairs(Values) do
         if not B then
            Acquiered = false --Works awsomely
         end
      end
   end)
end

Weirdly, yesterday the script was able to register the values changing from a For loop using B.Changed. Now it doesn’t work. How should I have the function run every time a value is changed?

EDIT: I have also tried

function CheckValue()
   --function
end

for A,B in pairs(script.Values:GetChildren()) do
   B.Changed:Connect(CheckValue)
end

If you’re changing the values on the client then those changes will not replicate to the server and thus any server scripts listening for their changes with “:GetPropertyChangedSignal()” or “.Changed” will not detect any changes.

The issue with this implementation is that anything which isn’t “nil” or which isn’t “false” is considered truthy, i.e; 1, 2, 3, “a”, “b”, “c” etc.

Here’s a function which determines if the values of an array or a dictionary are truly true, it will return true if so, false if not and nil if the argument passed to it is not a table value.

local function isAllTrue(tables)
	if type(tables) == "table" then
		for key, value in pairs(tables) do
			if type(value) == "boolean" then
				if not value then
					return false
				end
			else
				return false
			end
		end
		return true
	end
end
	
local isTableTrue = isAllTrue{["1"] = true, ["2"] = true, ["3"] = true}
print(isTableTrue) --true

Instead of checking if the elements are true, you could instead check if any one is false

local hasFalse = false
for i=1,#ThisTable do
   if not ThisTable[i] then
      hasFalse = true
      break
   end
end
if not hasFalse then
  -- function
end

Sorry it was already answered oof

The changes are made on the server, which the client is able to listen too. Other scripts are using this same method, and are working just fine.

I didn’t infer that in that scenario they wouldn’t work. I was referring to changing the values on the client (as that wouldn’t replicate to the server).

1 Like