What do you want to achieve? Keep it simple and clear!
I want to check if a value in a table is true.
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.
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.
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
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
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
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
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
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
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:
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
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.
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: