Help with Datastore 2!

Hi! I need help with specific saving on datastore 2. I am able to save predetermined values like the players gold or Xp but how about when there is a random value? Like saving the player’s items or quests. How do I save the items the player obtains and the quests they take? (If you dont get what I mean, I can save a value like gold because it will just save whatever is being recorded as the value, but how about saving specific items like lets say, apples oranges and pears out of 20 fruits when the player has only collected oranges. I am unable to predetermine what the player will actually pick so I am confused on how to save it properly.)

1 Like

I dont know if this is of help but you will need to save tables. It works the exact(I think) same way as regular values but it allows you to store more data

local StoredTableExample = {
 CurrentQuest = Quest7,
 QuestCompleted = {Quest1,Quest5,Quest2},
 Gold = 548,
 Inventory = {
  Item1 = {Rare,500,70},
  Item2 = {Common,48,99}}
}

Im not 100% if this is the best way but it’s here of what to do or what not to do.

I’ve not really saved tables before, mostly just plain values. Could you give me a small detailed guide on how to exactly save tables?

Im not sure how to exactly explain it. Save a table is the same as a normal variable

local Variable = 69
local Table = {69}

DataStore:Save(Variable)
DataStore:Save(Table)

DataStore:Get(Variable)
DataStore:Get(Table)

The difficult part is packing and unpacking the table. I cannot help you there, but you will need to take different values and place tables inside of table and save that main table as a variable.

You’ll have to use for loops and use a predetermined root folder in order to save all the data.

I’ll explain in pseudo-code.

In the loading function you use a for loop to create all the values in a folder, I’ll call this folder, “Data”.

What you could do is save your data as a dictionary and unpack the values in your save function.

In your save function you can use a GetChildren or GetDescendants loop to look through all the items in “Data”, and convert them into more primitive types like numbers, strings, booleans, tables, etc…

You can look at my Instance Parser code to get a better idea of what you could do, but it uses vanilla data stores.