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
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.
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.
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)
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.
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)