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
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
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 ^_^ā
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)
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.
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!