How to turn an Array into a Dictionary

I’m having trouble adding a new dict to a pre-existing save in a datastore… So this is what I’m doing:

local Data = {}
table.insert(Data, GetData) --2022 data
table.insert(Data, DefaultTable) --2023 data
pcall(function()
	ActivityData:SetAsync(Player.UserId, Data) --setting it
end)

When doing this, the datastore changes to this:
image

When really that “1)” and “2)” shouldn’t be there… How would I remove that?

Btw, this is what it should look like:
image

2 Likes

To set a key and value inside a dictionary, you would just do:

local t = {}
t.index = "value"

print(t.index) --> prints `value`

In your case, you would set the year as the key:

local data = {}
data[year] = <data>

table.insert inserts items into a table like an array.

2 Likes

you can use next() to get the index(year) and value(2023 data)
so to have the table {[2023] = something, [2023] = something}, I’d do this

local data = {}
table.insert( data, next(GetData) )
table.insert( data, next(DefaultTable) )

next returns the next (or first in this case), key and value, so it’s the same as inputting the 2023 and the 2023 data into table.insert

1 Like

That isn’t an error but a warning, saying that next() can return multiple values which could break table.insert(). In this situation it won’t.

But table.insert() is for arrays, so that doesn’t make any sense. You have to do what HugeCoolboy2007 said.

local data = {}
data["2022"] = GetData
data["2023"] = DefaultTable

Or like this

local data = {}
data["2022"] = GetData["2022"]
data["2023"] = DefaultTable["2023"]

-- or

function mergeDicts(a, b)
	for k,v in b do
		a[k] = v
	end
end

local data = {}
mergeDicts(data, GetData)
mergeDicts(data, DefaultTable)

I’m not quite sure what structures GetData and DefaultTable have

to mask that, you could probably save each next(someTable) to variables
local index, value = next(someTable)

also, @ketrab2004, table.insert does work in dictionaries

DefaultTable:

Looks like that.
And GetData is the same thing but something like this (example)

local DefaultTable = {
	[”2022”] = {
		[“January”] = {
		        [”5”] = {
				["ItemsSold"] = 5,
				["CommandsUsed"] = 5,
				["Hours"] = 5,
				["Minutes"] = 5,
				["Seconds"] = 5
			},
                        [”6”] = {
				["ItemsSold"] = 5,
				["CommandsUsed"] = 5,
				["Hours"] = 5,
				["Minutes"] = 5,
				["Seconds"] = 5
			}
		},
                [“March”] = {
		        [”5”] = {
				["ItemsSold"] = 5,
				["CommandsUsed"] = 5,
				["Hours"] = 5,
				["Minutes"] = 5,
				["Seconds"] = 5
			},
                        [”6”] = {
				["ItemsSold"] = 5,
				["CommandsUsed"] = 5,
				["Hours"] = 5,
				["Minutes"] = 5,
				["Seconds"] = 5
			}
		}
	}
}
1 Like

image
I didn’t know that you could choose a key/pos like that, but as you can see it’s for arrays.

In that case you could do either

The second would be more dynamic.

1 Like

With this, I can just save data to the DataStore and it wouldn’t do the “1)” and “2)” thing, correct?

This:

pcall(function()
	ActivityData:SetAsync(Player.UserId, data)
end)

Yes, but you would have to replace "2023" with

for it to work next year.

1 Like

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