Returning Value From Function

Hi, this is kinda of confusing to explain, but bear with me.

local Table = {
	Var1 = false;
	Var2 = false;
};

local function CheckTable(TableToCheck)
	for i,v in pairs(TableToCheck) do
		if v == false then
			return i
		end
	end
end

local VariableThatsEqualToFalse1, VariableThatsEqualToFalse2 = CheckTable(Table)

print(VariableThatsEqualToFalse1, VariableThatsEqualToFalse2) -- Output: "Var1  nil"

I loop through the table and check if something is equal to false. In this case there are 2 things that are equal to false in my Table, that would be Var1 and Var2. in my function I returned i so I could see which Variables in the table was equal to false. It should have printed out “Var1 Var2” but it printed out “Var1 nil”. How would I get it to print out “Var1 Var2”? Or in other words how would I get the function to return all names of the variables that are equal to false?

local Table = {
	Var1 = false;
	Var2 = false;
};

local function CheckTable(TableToCheck)
	for i,v in pairs(TableToCheck) do
		if v == false then
			return i
		end
	end
end

print(CheckTable(Table))

This only prints out “Var1”. But nice try!

I can fix that easy
let me redo it

Alright. I’ll be looking then.

Why not instead of returning it just add all key’s to a array and return that?

What do you mean, can you show me an example?

Wouldn’t a table be better, because you won’t always know how many things are false with a tuple.

Create an additional table:

local table2 = {}

And instead of the return replace it with a:

table.insert(table2, i)

Alright, let me try that. I’ll let you know if it works.

local Table = {
	Var1 = false;
	Var2 = false;
};

local function CheckTable(TableToCheck)
        local Total = {}
	for i,v in pairs(TableToCheck) do
		if v == false then
		        table.insert(Total, i)
		end
	end
        return Total
end

print(CheckTable(Table))

I’m on mobile so I’m pretty slow rn
I literally returned a table

1 Like

Alright, that worked! Thank’s for the help

damn the spaces on this is so messed up

Your idea also worked, Thanks!