Annoying table.unpack inconsistency issue

Hello! I’m working on a data serializer atm for my grid based building game and its been going flawlessly till I noticed something weird:

It seems like adding something in a table after unpacking a table gets rid of all the contents of the unpacked table except the very first value… :man_facepalming:

Could this be a glitch or an unintended thing or is there something I’m not aware of?

1 Like

Apparently when you unpack it, the entire table is now a tuple. This means they ignore the separators of the original table and use the first value, if there are other values ahead?

This is strange. It also happens on Lua in general.

local tab = {2, 2, 2, table.unpack({3, 3, 3, 3}), 2, 4}

print(table.concat(tab)) -- prints 222324, even on Lua outside Roblox
print(table.unpack({3, 3, 3, 3})) -- prints 3 3 3 3

It is possible that it is specifying that 4th index should contain one element from the unpacking and 5th and 6th elements are occupied. Quite logical. This means that you’ll have to work a little around this issue if your table ever ends with other series of numbers.

1 Like

Do you understand how multiple returns work in Lua? Function calls and vararg expressions not in the last result of a table literal or multiple return syntax will always result in the first value the function returned (or nil if the function returns no value). This is intentional.

They’re not “tuple” objects. They’re stocked in the stack C-wise IIRC (but I might be wrong). If Lua returns multiple value, they really return multiple value, they’re not packed into a single data structure when you unpack a table.

1 Like

It is actually a tuple by definition.

You can just pack it again. Although you could opt for the term elements instead for multiple values.

It is not a bug, just a quirk of Lua syntax.

Read this great article about it:

So basically I have to make sure its at the very end otherwise they’ll just get cut off even if the value after is literally empty?

Yes, as @Blockzez was saying:

local function mult()
    return 1, 2, 3

print(mult()) -- 1 2 3
print(mult(), nil) -- 1 nil