Having issues with returning values from for loop

Ok so basically i wanted to loop trough a table and then return the value, but it doesnt print the whole table, it prints only 1 value of the table

local example = {1,2,3}

function LoopTroughTable(Table)
	if Table ~= nil then	
		for i, loop in ipairs(Table) do
           return loop

		end
	end
end
local LoopedTable = LoopTroughTable(example) --i call the function here
print(LoopedTable) --only prints 1
2 Likes

That’s because you’re stopping the loop

3 Likes

okay, but how do i fix this?

This text will be blurred

Just put the print inside the for loop.

local example = {1,2,3}

function printTableValues(Table)
	if Table ~= nil then	
		for i, v in ipairs(Table) do
           print(v)
		end
	end
end

printTableValues(example) -- i call the function here
2 Likes

Sorry for edit, misinterpreted question initially somehow

2 Likes

this just loops trough a table then puts it back i a table, resulting it in printing: table: x8f590fada712b28e

no you don’t get it, i wan’t to use the variable ā€˜loopedTable’ later on,

it’s hard to explain, but LoopedTable currently prints only one part of the table, not the entire table.
the solution isn’t removing the variable or putting print() inside the loop

In what form do you want the values? Because if you loop through the table, and get all the values, why not just use the original table? I am slightly confused by what you are trying to achieve ^_^’

1 Like

Maybe do this?

local example = {1,2,3}

function LoopTroughTable(Table)
	if Table ~= nil then	
		for i, loop in ipairs(Table) do
            print(loop)

		end
        return Table
	end
end
local LoopedTable = LoopTroughTable(example)
1 Like

Nope, it just prints: table: yr7j437y2n

you can’t print the table without getting something like that

What I’m saying to do is to still return the table, but inside the loop, print each individual value.

Because a table is returned, you can’t have multiple values (without returning a table!) unless you do the following:

local example = {1,2,3}

function loopThroughTable(Table)
    return Table[1], Table[2], Table[3]
end

local loopedTable1, loopedTable2, loopedTable3 = loopThroughTable(example)
print(loopedTable1, loopedTable2, loopedTable3)

You could do this, but this only works for up to three values, and I’m still confused at what you want specifically.

1 Like

It Works!
I think the way i tried to do it isn’t actually possible
i used a seperate function because i thought it would result in some cleaner code.
Ty for the help everybody!

1 Like

A function can return any number of values.

local function getV(t)
	return t[1], t[2], t[3], t[4]
end

local v1, v2, v3, v4 = getV({"a", "b", "c", "d"})
print(v1, v2, v3, v4) --a b c d

You can use table.unpack/unpack to achieve this.

local function printT(t)
	print(table.unpack(t)) --a b c d
end

printT({"a", "b", "c", "d"})

You have misinterpreted what I wrote…
The code I provided only works with 3 values. I know you can just add more.

(Cannot help but think you were just eager for a ā€œgotchaā€ reply, correct me if I am wrong of course haha)

Yes, but the OP wanted variable references to the values, not simply to print them.

Yes, but the OP wanted variable references to the values, not simply to print them.

but it doesnt print the whole table, it prints only 1 value of the table

You could do this, but this only works for up to three values

This implies that multi-assignment expressions can only handle the assignment of three variables.