How do I check if all variables in a table are true?

I have a table with 4 variables, when all 4 of them are true i want it to fire a event, how would I do this?

local trueVariables = 0 -- remember to set it 0 before code below
for i, variable in pairs(table) do -- iterate throught each variable in table
   if variable == true then -- check if it true
      trueVariables = trueVariables + 1
   end
end

if trueVariables == #table then -- if true amount = variables amount then
    -- fire event here
end

You can make a function to do this for you

local function checkTable(t)
	local check = true 
	
	for _, v in pairs(t) do --let's loop through the table
		if v ~= true then --if this value isn't true then
			check = false --set check variable to false
			break --stop the loop
		end
	end
	
	return check --returns check variable
end

local example = {true, true, false}
local example2 = {true, true, true}

print(checkTable(example)) --> false
print(checkTable(example2)) --> true
1 Like

hmm, yes, if you need ALL values to be true then this is option

If it’s only true or false, can’t you just do this?

if table.find(t,false) then
    -- found false value
else
    -- all values are true
end
3 Likes

I tried this but it just prints “All values are true” every time I press a button. Heres the script:

local ProximityPromptService = game:GetService("ProximityPromptService")
local button = script.Parent
local OpenDoorEvent = script.Parent.Parent.OpenDoorEvent
local CloseDoorEvent = script.Parent.Parent.CloseDoorEvent
local b1 = script.Parent.Button1
local b2 = script.Parent.Button2
local b3 = script.Parent.Button3
local b4 = script.Parent.Button4

local p1 = b1.ProximityPrompt
local p2 = b2.ProximityPrompt
local p3 = b3.ProximityPrompt
local p4 = b4.ProximityPrompt

local a1 = false
local a2 = false
local a3 = false
local a4 = false
local ProxPrompts = {p1, p2, p3, p4}


function FindFalse()
	if table.find(ProxPrompts,false) then
		print("All values are not true")
	else
		print("All values are true")
	end
end

p1.Triggered:Connect(function()
	b1.BrickColor = BrickColor.new("Lime green")
	a1 = true
	p1:Destroy()
	wait()
	FindFalse()
end)

p2.Triggered:Connect(function()
	b2.BrickColor = BrickColor.new("Lime green")
	a2 = true
	p2:Destroy()
	wait()
	FindFalse()
end)

p3.Triggered:Connect(function()
	b3.BrickColor = BrickColor.new("Lime green")
	a3 = true
	p3:Destroy()
	wait()
	FindFalse()
end)

p4.Triggered:Connect(function()
	b4.BrickColor = BrickColor.new("Lime green")
	a4 = true
	p4:Destroy()
	wait()
	FindFalse()
end)

You have your table filled with proximity prompt references, so it will never find the value false in there, causing your else block to execute every time. (did you mean store your ‘a’ variables instead?)

local flags = {a1 = false, a2 = false, a3 = false, a4 = false}

local function allFlagsTrue()
for _, state in flags do
if not state then return end
end

return true
end

function FindFalse()
	if not allFlagsTrue() then
		print("All values are not true")
	else
		print("All values are true")
	end
end

create a function…

function Check(Table)
 for i, v in pairs(Table) do
  if not v then
   return false
  end
 end
end)

a and connect this function…

I tried that but it still only prints All values are not true every time?

local ProximityPromptService = game:GetService("ProximityPromptService")
local button = script.Parent
local OpenDoorEvent = script.Parent.Parent.OpenDoorEvent
local CloseDoorEvent = script.Parent.Parent.CloseDoorEvent
local b1 = script.Parent.Button1
local b2 = script.Parent.Button2
local b3 = script.Parent.Button3
local b4 = script.Parent.Button4

local p1 = b1.ProximityPrompt
local p2 = b2.ProximityPrompt
local p3 = b3.ProximityPrompt
local p4 = b4.ProximityPrompt

local a1 = false
local a2 = false
local a3 = false
local a4 = false	
local flags = {a1 = false, a2 = false, a3 = false, a4 = false}

local function allFlagsTrue()
	for _, state in flags do
		if not state then return end
	end

	return true
end

function FindFalse()
	if not allFlagsTrue() then
		print("All values are not true")
	else
		print("All values are true")
	end
end


p1.Triggered:Connect(function()
	b1.BrickColor = BrickColor.new("Lime green")
	a1 = true
	p1:Destroy()
	FindFalse()
end)

p2.Triggered:Connect(function()
	b2.BrickColor = BrickColor.new("Lime green")
	a2 = true
	p2:Destroy()
	FindFalse()
end)

p3.Triggered:Connect(function()
	b3.BrickColor = BrickColor.new("Lime green")
	a3 = true
	p3:Destroy()
	FindFalse()
end)

p4.Triggered:Connect(function()
	b4.BrickColor = BrickColor.new("Lime green")
	a4 = true
	p4:Destroy()
	FindFalse()
end)

try this? ---------------------

I tried it but now it prints all are true every time?

local ProximityPromptService = game:GetService("ProximityPromptService")
local button = script.Parent
local OpenDoorEvent = script.Parent.Parent.OpenDoorEvent
local CloseDoorEvent = script.Parent.Parent.CloseDoorEvent
local b1 = script.Parent.Button1
local b2 = script.Parent.Button2
local b3 = script.Parent.Button3
local b4 = script.Parent.Button4

local p1 = b1.ProximityPrompt
local p2 = b2.ProximityPrompt
local p3 = b3.ProximityPrompt
local p4 = b4.ProximityPrompt

local a1 = false
local a2 = false
local a3 = false
local a4 = false	
local ProxPrompts = {a1 = false, a2 = false, a3 = false, a4 = false}
local trueVariables = 0 -- remember to set it 0 before code below

function FindFalse()
	for i, variable in pairs(ProxPrompts) do -- iterate throught each variable in table
		if variable == true then -- check if it true
			trueVariables = trueVariables + 1
		end
	end

	if trueVariables == #ProxPrompts then -- if true amount = variables amount then
		print("all are true")
	end
end	






p1.Triggered:Connect(function()
	b1.BrickColor = BrickColor.new("Lime green")
	a1 = true
	p1:Destroy()
	FindFalse()
end)

p2.Triggered:Connect(function()
	b2.BrickColor = BrickColor.new("Lime green")
	a2 = true
	p2:Destroy()
	FindFalse()
end)

p3.Triggered:Connect(function()
	b3.BrickColor = BrickColor.new("Lime green")
	a3 = true
	p3:Destroy()
	FindFalse()
end)

p4.Triggered:Connect(function()
	b4.BrickColor = BrickColor.new("Lime green")
	a4 = true
	p4:Destroy()
	FindFalse()
end)

wrong,
image

Like this?

local ProximityPromptService = game:GetService("ProximityPromptService")
local button = script.Parent
local OpenDoorEvent = script.Parent.Parent.OpenDoorEvent
local CloseDoorEvent = script.Parent.Parent.CloseDoorEvent
local b1 = script.Parent.Button1
local b2 = script.Parent.Button2
local b3 = script.Parent.Button3
local b4 = script.Parent.Button4

local p1 = b1.ProximityPrompt
local p2 = b2.ProximityPrompt
local p3 = b3.ProximityPrompt
local p4 = b4.ProximityPrompt

local a1 = false
local a2 = false
local a3 = false
local a4 = false	
local ProxPrompts = {a1 = false, a2 = false, a3 = false, a4 = false}

function FindFalse()
	local trueVariables = 0 -- remember to set it 0 before code below
	for i, variable in pairs(ProxPrompts) do -- iterate throught each variable in table
		if variable == true then -- check if it true
			trueVariables = trueVariables + 1
		end
	end

	if trueVariables == #ProxPrompts then -- if true amount = variables amount then
		print("all are true")
	end
end	






p1.Triggered:Connect(function()
	b1.BrickColor = BrickColor.new("Lime green")
	a1 = true
	p1:Destroy()
	FindFalse()
end)

p2.Triggered:Connect(function()
	b2.BrickColor = BrickColor.new("Lime green")
	a2 = true
	p2:Destroy()
	FindFalse()
end)

p3.Triggered:Connect(function()
	b3.BrickColor = BrickColor.new("Lime green")
	a3 = true
	p3:Destroy()
	FindFalse()
end)

p4.Triggered:Connect(function()
	b4.BrickColor = BrickColor.new("Lime green")
	a4 = true
	p4:Destroy()
	FindFalse()
end)
1 Like

yes ---------------------------------

Hmm i tried but it still prints all are true every time i press a button?

… try to
local ProxPrompts = {a1, a2, a3, a4}

Now it just does nothing

local ProximityPromptService = game:GetService("ProximityPromptService")
local button = script.Parent
local OpenDoorEvent = script.Parent.Parent.OpenDoorEvent
local CloseDoorEvent = script.Parent.Parent.CloseDoorEvent
local b1 = script.Parent.Button1
local b2 = script.Parent.Button2
local b3 = script.Parent.Button3
local b4 = script.Parent.Button4

local p1 = b1.ProximityPrompt
local p2 = b2.ProximityPrompt
local p3 = b3.ProximityPrompt
local p4 = b4.ProximityPrompt

local a1 = false
local a2 = false
local a3 = false
local a4 = false	
local ProxPrompts = {a1, a2, a3, a4}

function FindFalse()
	local trueVariables = 0 -- remember to set it 0 before code below
	for i, variable in pairs(ProxPrompts) do -- iterate throught each variable in table
		if variable == true then -- check if it true
			trueVariables = trueVariables + 1
		end
	end

	if trueVariables == #ProxPrompts then -- if true amount = variables amount then
		print("all are true")
	end
end	






p1.Triggered:Connect(function()
	b1.BrickColor = BrickColor.new("Lime green")
	a1 = true
	p1:Destroy()
	FindFalse()
end)

p2.Triggered:Connect(function()
	b2.BrickColor = BrickColor.new("Lime green")
	a2 = true
	p2:Destroy()
	FindFalse()
end)

p3.Triggered:Connect(function()
	b3.BrickColor = BrickColor.new("Lime green")
	a3 = true
	p3:Destroy()
	FindFalse()
end)

p4.Triggered:Connect(function()
	b4.BrickColor = BrickColor.new("Lime green")
	a4 = true
	p4:Destroy()
	FindFalse()
end)
 function FindFalse()
	local trueVariables = 0 -- remember to set it 0 before code below
	for i, variable in pairs(ProxPrompts) do -- iterate throught each variable in table
		if variable == true then -- check if it true
			trueVariables = trueVariables + 1
		end
	end

    print(trueVariables)

	if trueVariables == #ProxPrompts then -- if true amount = variables amount then
		print("all are true")
	end
end

That just prints “0” every time i press a button?

local ProximityPromptService = game:GetService("ProximityPromptService")
local button = script.Parent
local OpenDoorEvent = script.Parent.Parent.OpenDoorEvent
local CloseDoorEvent = script.Parent.Parent.CloseDoorEvent
local b1 = script.Parent.Button1
local b2 = script.Parent.Button2
local b3 = script.Parent.Button3
local b4 = script.Parent.Button4

local p1 = b1.ProximityPrompt
local p2 = b2.ProximityPrompt
local p3 = b3.ProximityPrompt
local p4 = b4.ProximityPrompt

local a1 = false
local a2 = false
local a3 = false
local a4 = false	
local ProxPrompts = {a1, a2, a3, a4}

function FindFalse()
	local trueVariables = 0 -- remember to set it 0 before code below
	for i, variable in pairs(ProxPrompts) do -- iterate throught each variable in table
		if variable == true then -- check if it true
			trueVariables = trueVariables + 1
		end
	end

	print(trueVariables)

	if trueVariables == #ProxPrompts then -- if true amount = variables amount then
		print("all are true")
	end
end






p1.Triggered:Connect(function()
	b1.BrickColor = BrickColor.new("Lime green")
	a1 = true
	p1:Destroy()
	FindFalse()
end)

p2.Triggered:Connect(function()
	b2.BrickColor = BrickColor.new("Lime green")
	a2 = true
	p2:Destroy()
	FindFalse()
end)

p3.Triggered:Connect(function()
	b3.BrickColor = BrickColor.new("Lime green")
	a3 = true
	p3:Destroy()
	FindFalse()
end)

p4.Triggered:Connect(function()
	b4.BrickColor = BrickColor.new("Lime green")
	a4 = true
	p4:Destroy()
	FindFalse()
end)

remove local ProxPrompts = {a1, a2, a3, a4} and paste in there. this was the problem

 function FindFalse()
	local trueVariables = 0 -- remember to set it 0 before code below

    local ProxPrompts = {a1, a2, a3, a4} -- replace

	for i, variable in pairs(ProxPrompts) do -- iterate throught each variable in table
		if variable == true then -- check if it true
			trueVariables = trueVariables + 1
		end
	end

    print(trueVariables)

	if trueVariables == #ProxPrompts then -- if true amount = variables amount then
		print("all are true")
	end
end
1 Like