Unpack() only partially unpacks?

local success, errormessage = pcall(function()
	local Data = game:GetService("DataStoreService"):GetDataStore("StoredPlayerData02"):GetAsync(player.UserId)
	


	if Data ~= nil then
		local finalData = unpack(Data)
		print(finalData["UNVER_VEED"])
		print(finalData["VER_VEED"])
	end
end)

Table I want to unpack:

{
	{["UNVER_VEED"] = 74},
	{["VER_VEED"] = 0}
}

It should print 74 and 0however it only prints 74 and the [“VER_VEED”] Key as nil
image

What I basically wanna do is saving all Values in the leaderstats folder to a datastore.

When you unpack the data, it should work like this:

local a, b = table.unpack(Data)

print(a["UNVER_VEED"]) -- 74
print(b["VER_VEED"]) -- 0

It sort of becomes a tuple after you unpack it.

1 Like

Thats a solution but pretty unpratical for my use. As I will have many more data in the future and it would be annoying to always add new variables.

I sorta need it like that it converts this table:

{
   {
       ["UNVER_VEED"] = 74
   },
   {
       ["VER_VEED"] = 0
   },
}

Into this one:

{
	{"UNVER_VEED"] = 74,
	["VER_VEED"] = 0
}

That was basically my Idea of how unpacking works haha.

Thanks for the help anyway, learned something new. ;=)

Then why don’t you do something like this?:

local unpackTable = function(t)
    local returnValue = {};

    for _,v in pairs(t) do --Loop through the given table to get the other tables
        for k,v in pairs(v) do --Loop through all the keys and values of the table
            returnValue[k] = v; --Set the key and value to another table
--          Doing this will remove the levels by setting all keys in one table
        end
    end

    return returnValue; --Return the new table
end
1 Like

Thanks, this seems pretty logical. Could have come to this idea myself too haha

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.