How to use DataStore2 - Data Store caching and data loss prevention

How will I get an offline player’s data?
I’m pretty sure there is a way to get the data with DataStoreService cause the module uses that service right? I just need to know how to do it.
I know about DATA/123456 but how do I get the actual data cause I use the combine thing.

Are all your keys combined under one key?

2 Likes

If you use Combine, then once you get the data you’ll receive a dictionary. The keys are the keys you combine under.

2 Likes

Ideally you would have the strings "Aa", "Ab", "Ac", ... in a table under the key "Items". I am pretty sure this would be undefined behaviour as well, because when you do DataStore2("Items", player) what are you referring to

I am trying to make a system where you can buy multiple of the same things and still save them for the next time you rejoin but I do not want them to be tools.
So I just want to have something like a category data to store all other smaller data in the category data. So that I do not clog my main Combine with all the smaller data

they are combined. It seems solved for now.
An advice for everyone that has the same problem: read the api carefully, not like me xD
Instead of just saving and teleporting, call the teleport on AfterSave of the data you want to save

Is DataStore2 capable of saving nested tables? I seem to be having issues with that. Any help is welcome, thanks. @Kampfkarren

So do I get data like DATA/UserId[]?

Edit: it’s not supposed to be real code, just the idea of it.

Ok um let’s say I saved “cash” under “DATA”
Will this do

DataStoreService:GetDataStore("DATA/" .. tostring(player.UserId))["cash"]

or is there something else?
I don’t really know a lot about datastores 'cause I always used DataStore2 xd

2 Likes

how do I save string value names in datastore 2

1 Like

You don’t need to think about how Combine works.

Rule of thumb is to use one key, and just store every key you use under it. Unless you’re doing DataStore2("Aa", player), then you shouldn’t put it in the combine.

The wiki has guides on how to use standard data stores that you should reference.

1 Like

As stated countless times in the thread, tables are not different from any other saved value.

1 Like

How can I use Datastore2 for saving weapons?, A way I think I could do it is by saving the string name but how can I add it into a table using DataStore2? Could you also give me some examples please? Thank you!

1 Like

Any ideas why it isn’t saving then?

1 Like

You could assign each weapon an id and then add the owned weapons ids to a table that you could then save.

1 Like

I did thought of that but I’m using the weapons name but I’m not sure how to use GetTable and how to apply the OnUpdate() to it so it would save the name of the weapons into the table. I have a script that save the weapon but I’m not quite sure how to use UpdateAsync so I’m planning to use datastore2 for it so the data isn’t lost. Besides, the script I used, I also need to wait 6 seconds before saving another item into the table which is quite annoying.

1 Like

With such little information, the only thing I can tell you is it is a problem with your code.

1 Like

I am trying to better understand how DataStore2 works and I am not sure if the way I think it saves and indexes data is right.

Whenever I want to wipe data I use the following function you wrote in this thread to wipe data from a player for GDPR:

local DataStoreService = game:GetService("DataStoreService")
function clear(userId, name)										--IF COMBINED: NAME IS MASTERKEY
	local orderedDataStore = DataStoreService:GetOrderedDataStore(name .. "/" .. userId)
	local dataStore = DataStoreService:GetDataStore(name .. "/" .. userId)

	while true do
		local pages = orderedDataStore:GetSortedAsync(false, 100)
		local data = pages:GetCurrentPage()
		for _, pair in pairs(data) do
			print(("key: %d"):format(pair.key))
			dataStore:RemoveAsync(pair.key)
			orderedDataStore:RemoveAsync(pair.key)
		end
		if pages.IsFinished then
			print(("finished (%d: %s)"):format(userId, name))
			return
		end
	end
end
clear(PlayerId, DatastoreName)

I have noticed that, the more times I play or test the game, and wipe my data, there are more and more keys which are being deleted.

Because Datastore2 uses orderedDatastores to save backups of playerdata, I assume each key refers to a backup of the playerdata saved in a normal datastore which is saved whenever data is called in a game for the first time.

Is it true that every backup is saved in a normal datastore, while the keys to access this backup are retrieved by getting it from the orderedDatastore?

1 Like

This post by berezaa explains the method:

2 Likes