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

As far as I know, there is no way to do this currently.

When I click stop in studio I get “script that implemented this callback has been destroyed”. A friend also implemented DS2 in his tyccon and he said that my code has no problems (that he knows of) that should give this error.

The details of code that I used for DS2 are in this post.

Does ClearCache wipe all the data of every data store? If so, it doesn’t seem to work for me:

local DataStore2 = require(modules.DataStore2)
DataStore2.ClearCache()

What’s your use case? It’s more or less a function that’s just used for unit testing.

I want to wipe all my data stores and start with fresh ones.

That’s not what it does. Just pick a new master key if you want to do that.

I’m having some problems with this Datastore2 in my tycoon game. Although most of the time it works fine, almost every day I receive reports of data that is not saved when the user teleports to another place/leave the game.
*the “cash” value (a int) is saved everytime, with no exceptions. However candy coins (a seasonal token, another int) doesnt save most of the time
*gems (a int) are saved when they are bought by robux, however they are not saved like 50% of the times the player spend them on something.
*tycoon pieces (a array) are saved most of the time, but almost everyday i get a report of someone that lost a bought piece (althought the cash is saved so the player lose money and didnt got the piece xD)
im trying to debug this but there is no error or warning on the log. It just dont saves. Any suggestion please?

1 Like

I also have this problem sometimes, but luckily, for me, my game is not out yet and can either wait for this to be fixed or use an alternative to save the data before I release my game. Not sure why it is not savimg at random times though.

1 Like

Are we ever going to get a built-in function to clear data for DS2?

If you are referring to for GDPR, search “DataStore2 GDPR”.

Putting a table with circular references in the cache causes a stack overflow

Edit: My bad, it’s actually SaveAsync that is causing this

Well yeah, there is that, but I was wondering if you could build in some kind of way using someone’s userId (preferably) or their player object to do that. Most comparable to :RemoveAsync.

File an issue on GitHub after making sure you are on the latest release, I have not heard of this behavior.

I was reading your API… is it now recommended to use SaveAsync() instead of Set() ?

What? They do completely different things, I’d suggest reading the example again.

How i can use DataStore2 without a player.
For example load a house or smth like this.

I am just confused, I am combining 10 different keys to “DATA” I am storing different values like int, table and bool. I am using :Get() on all of them and it doesnt actually work it just simulates storing, but there is no saved data upon rejoining, it only works if there is only one key.
I discovered this in gotchas page:

  • Because of the throttles on OrderedDataStores, DataStore2 (with the default saving method) is only guaranteed to not throttle on :Get() if you use less than 2 unique keys.

I assume my data is broken of that reason, which method should I use instead of Get() ? I also dont want to be forced to set defoult value as a parameter if possible

You can’t. Datastore2 is attached to the player instance. If you want to do datastores for house, etc I recommend ProfileService by @loleris

1 Like

If they’re all combined under one DATA key, then this limitation does not apply to you. If they’re not, then you are not using the module correctly.

I need help with understanding :OnUpdate()
So I wrote some code to test ds2 and I am confused with what I got,

local datastore2 = require(script.Parent:WaitForChild("DataStore2"))
datastore2.Combine("DATA", "money", "codes")

game.Players.PlayerAdded:Connect(function(plr)
	local money = datastore2("money", plr)
	money:OnUpdate(print(money:Get()))
	money:Set(money:Get(0) + 1)
	--------------------------------
	local codes = datastore2("codes", plr)
	codes:OnUpdate(print(table.unpack(codes:GetTable({}))))
	local new = codes:GetTable({})
	print(#new)
	new[#new + 1] = "n"
	codes:Set(new)
end)

Its confusing because it actually prints this:
image
which means that OnUpdate was ran before print(#new), eventhough the line where I am changing datastore is a few lines below!

1 Like