The title says it all. I’m trying to use a table to see if multiple values are set to true. I’ve tried things like for _,v in pairs(table) do if v then print('Nice') end end
but that didn’t work, I also tried something like if value and value2 and value3 then print('Nice') end
but that also didn’t work.
local function checkValues(table)
local v1 = false -- previous value
for _, v in pairs(table) do
if not v1 and v then -- only 1 value then
v1 = v -- set true
elseif v1 and v then -- 2 values then true
return true
end
end
return false
end
Note, this returns true for 2+ values, so this includes 3, 4 etc as well. If you want to instead check that you have to use 2 values and use a 3rd value and the following code:
v1 and v2 and not v3
idk the purpose of the system, but if all tables are dictionaries would be easy to check everything with one loop without having a previous V1 data.
local Dic1 = {}
Dic1[player.UserId] = true
local Dic2 = {}
Dic2[player.UserId] = false
for _, p in pairs(Players:GetPlayers()) do
if Dic1[p.UserId] and Dic2[p.UserId] then
print("Both are true")
end
end
I can’t get this script to function. It gives an error about my table. How would the table used here look like?
If you want to check if all the values in a table are true, you could just do this:
local sample = {true, true, true, false}
local true_count = 0
for _, v in pairs(sample) do
if v == true then
true_count = true_count + 1
end
end
if true_count == #sample then
print("All values are true")
else
print("Not all values are true")
end
I’m pretty sure that the code explains it all .
there where two mistakes in the code. After fixing those minor mistakes the code worked. Thank you for helping!
A much easier way is just to do the following:
local YourTable = {false,true,true,false}
if not table.find(YourTable, false) then
--Your code to run
else
--Your code to run if there is a false value.
end
This will run even when not all of them are set to true
Just remove the else part, if you do not want to do anything if one or more is false.
Like this:
local YourTable = {false,true,true,false}
if not table.find(YourTable, false) then
--Your code to run
end
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.