Storing returned value as a variable

Let’s say I have a simple module that’s supposed to return an unpacked table.

  local module = {}
 
       function module.unpack(tab)
           return table.unpack(tab)
       end

  return module

Now when I require the module and call the function :

   local module = require(game.ServerScriptService.ModuleScript)  
   local arr = {1,2,3,4,5}

   print(module.unpack(arr)) ---> prints the table unpacked

But when I attempt to store the returned unpacked table as a variable,
   local module = require(game.ServerScriptService.ModuleScript)  
   local arr = {1,2,3,4,5}

   unpacked =  module.unpack((arr))
   print(unpacked) ---> only prints  1 , which is the first element of the table

How can I store the unpacked table correctly?

1 Like

Hey,
table.unpack returns a tuple.

https://devforum.roblox.com/t/what-does-table-pack-table-unpack-do/388613/8

You can put brackets around the unpacked variable to turn it into a table. (pretty sure not confident)
Example:

local module = require(game.ServerScriptService.ModuleScript)  
local arr = {1,2,3,4,5}

unpacked =  {module.unpack((arr))}
print(unpacked)
2 Likes

I found this about Lua’s behavior :

table.unpack returns multiple values. The defined behavior in that case is that if it is not the last one in a list of expressions then all but the first returned value will be discarded.

image

A workaround would be to make the table the last argument, but how?

My case is not limited to this redundant module function, I think it is impossible to receive a tuple number of arguments and assign it one variable.

1 Like

I dont fully get what you are trying to do. If you are trying to get the last arguement of the table then there are multiple ways

You can create a table, and get the last index of that table.

local module = require(game.ServerScriptService.ModuleScript)  
local arr = {1,2,3,4,5}

unpacked =  {module.unpack((arr))}
print(unpacked[#unpacked]) -- would print 5

You can also assign variables for the tuple that table.unpack is returning.

local module = require(game.ServerScriptService.ModuleScript)  
local arr = {1,2,3,4,5}

local var1,var2,var3,var4,var5 =  module.unpack((arr))
print(var5) -- would print 5

For a visual calling module.unpack(arr) will return something like this (which is unprintable)

“1”,“2”,“3”,“4”,“5”

2 Likes

That is not my case.

By “passing a table as the final argument” I meant it as in passing the table as the final argument, not the final element of it as that would just ruin the purpose of the function, which is to return an unpacked table, not just an element.

My test was to be able to assign one variable to refer to an unpacked table, which is impossible as table.unpack() returns a tuple.

There is no way for a script to assign every value it’s on variable, unless I use keys in a dictionary to be associated with every value, creating indexes for every value .

The problem however with this is as that I could return the dictionary but still not be able to , on another store the collection of values as one string… unless I attempt to use table.concat() and separate each separate element with a whitespace (" ") and then return the string as the result.

2 Likes

In theory, my idea was perfect for my use case.

A function :

   function unpack(tab)
         local a = table.concat(tab, ", ")
    return a
end  
a = {1,2,1,4,5} b = unpack(a) 
print(b) ---> prints successfully 1, 2, 1, 4, 5

this avoids using table.unpack() and thus avoiding the error of assigning each value as just one variable.

However when I do this in a module:

local module = require(game.ServerScriptService.ModuleScript)  
local arr = {1,2,3,4,5}

local var = module.unpack(arr) --> shouldn't this be the returned string??

print(var)

My question is

why isn’t var = the returned string?

1 Like

table.unpack()
will return as variants.

local t1,t2 = table.unpack({1,2})
print(t1,t2)
--// Prints 1 and 2

If you’re looking for a string you can do thing:

local table = {1,2,3,4,5,6};

function converttostring(tableto)
    local stringto = "";
    for i,v in pairs(table) do
        stringto .. v;
    end
    return stringto;
end
2 Likes

I modified this a bit for efficiency and function to work with a module.

Edit much simpler:

setmetatable(t, {__tostring = function(self) 
    return table.concat(self, "") 
end})

local str = tostring(t)
2 Likes