Check if ALL Values are True in Table [SOLVED]

  1. What do you want to achieve? Keep it simple and clear!
    I want to check if a value in a table is true.

  2. What is the issue? Include screenshots / videos if possible!
    The issue is I have only figured out how to find if only my first value in my table is true.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried using find.table() but that did not work either, unless I did it wrong but I read up on it and also watched a couple videos and still didn’t get anywhere. :confused:

Here’s an example of my script. “Check” is a BoolValue btw.

local comics = {
game.Workspace["Comic 1"].Check.Value,
game.Workspace["Comic 2"].Check.Value,
game.Workspace["Comic 3"].Check.Value
}

if comics[1][2][3] == true then -- this only works if i remove [2] and [3]

	script.Parent.Parent.Script.Enabled = false
	warn("OFF")
end

You could create a function with a loop to check your tables, you send the table into it and the function iterates all values in the table, if any of those is false, then a the variable “Lock” becomes false, return false, if all were true returns true

local comics = {
	game.Workspace["Comic 1"].Check.Value,
	game.Workspace["Comic 2"].Check.Value,
	game.Workspace["Comic 3"].Check.Value
}

local function checkTable(tableToCheck)
	local Lock = true
	for k, c in pairs(tableToCheck) do
		if not c then
			Lock = false
		end
	end
	return Lock
end

local result = checkTable(comics)
if result then
   script.Parent.Parent.Script.Enabled = false
   warn("OFF")
end

5 Likes

try use this

local comics = {
	game.Workspace["Comic 1"].Check.Value,
	game.Workspace["Comic 2"].Check.Value,
	game.Workspace["Comic 3"].Check.Value
}

if comics[1] and comics[2] and comics[3] == true then 

	script.Parent.Parent.Script.Enabled = false
	warn("OFF")
end
1 Like

That would be ineficient, cause if you have a table with 200 entries you would end typing(?)
if comics[1] and comics[2] and comics[3] and comics[4] and comics[5] and comics[etc, etc 200 ?] == true then

2 Likes

yeah I was just thinking this too, I have 16 total values in the table and it would be kinda lengthy to have all those ands for all 16 values- But I still am not sure how to use the solution you came up with. Im struggling to understand how to use understand what the k, c means or what the lock does too :confused:

Here’s one quick example.

local ValueTable = {
    true, true
}

local function Check(Table)
    for _, Value in Table do
        if (Value) then
            continue
        else
            return false
        end
    end
    return true
end

if (Check(ValueTable)) then
    print('All values are true')
else
    print('All values are not true')
end
  • Edited.
2 Likes

k and c are just variables the script create everytime it checks each entry in your table.

That function exactly as the one @Xacima provided can be used many times for any table you send into the function.

By calling the table function read like this:
local result = checkTable(comics) -- you are sending the table you want to check

Then the function runs with that table in mind, checking each value, if any is false, it returns false and you get false into the variable result

You can do anything you want with result variable like, if result is true, then disable the script:

if result then
   script.Parent.Parent.Script.Enabled = false
   warn("OFF")
end
2 Likes

Oh sorry, your question, k and c are the index and the value inside a table, each time the loops repeats, it holds those into k and c variables, example:

local Just_A_Table = {"a","b","c"}

for k, c in pairs(Just_A_Table) do
	print(k, c) 
end

That script would print:
1 a
2 b
3 c

I used the letter k just because I call it “key” and c because your table contains comics

1 Like

AAAAH! Ok I got it! And it does work, thanks guys! I wish I could mark you both for the solution! Since you replied first I’ll mark that as the solution but really thanks all of you with the quick replies and the explanations as well, it helped a lot c:

1 Like

To add onto this, here’s an easy way to remember what is what:

for Index, Value in Table do
    -- //
end

Index is always first.
Value is always second.

2 Likes

Alrighty got it! Thanks sm! c:

2 Likes

If you want a code golfed answer, you can also use table.find:

local t = {true, false, true}
local t2 = {true, true, true}
local allTrue = not table.find(t, false) -- false
local allTrue2 = not table.find(t2, false) -- true
2 Likes

I suppose that would run faster, right? Than iterating?
A similar approach for Dictionaries exist?

wait so how would I apply this to my script? I am guessing table “t” would be my “comics” table??? Sorry im confused, esp cause Im still trying to understand how table.find works.

1 Like

Its simple, table.find(yourTable, whatYouWantToFind), it returns the index number of where that value is stored in the table, example, place “3”

*if the value exist in your table of couse, if it doesnt exist returns nil

yup, any table you want

table.find is usually a good approach when it comes to accessing arrays, it does not work with dictionaries.

Yup I know, actually I prefer to use dictionaries instead of arrays. Actually are easy to use cause its possible to find a key without iterating and deleteting keys its easier than deleting a key in an array. I was just asking if theres a way to find a false value inside a Dictionary in a simplier way, than interating

Omg ok! So I tried using table.find again and it worked! This is what I ended up using:

if table.find(comics,true) then
	script.Parent.Parent.Script.Enabled = false
end

edit: I had tried using table.find before but was doing table.find(comics,Value) instead and idk what other attempts and they weren’t working but thanks yall I understand it better now c:

1 Like

Its cleaner and simpler totally. Dont forget the iterations or Dictionaries when you need more complex tables too :yum:

1 Like

yeah! I’ll make sure to read up on tables, arrays, dictionaries, etc. bc ik they are useful esp for some of the stuff I want to do, thanks!

1 Like