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

Oh i don’t know actually how DataStore2 is working. As i know it should automatically save value when player leaves as you said. Could you give me a code sample to do what you said earlier and i can try it. I don’t know all methods of DataStore2 and i couldn’t find anything on documentation. Can i read the cached value with a method ?

You’d probably do something along the lines of this…

--your npc script

local PLRS = game:GetService("Players")
PLRS.PlayerRemoving:Connect(function(removingPlayer) -- when a player leaves
    -- check if plr is an attacker
    local isAttacker = statusFolder:FindFirstChild(removingPlayer.Name)
    if (isAttacker) then
        -- reward gold
    end
end)

Thanks for your help! I should try different ways to do it i guess.

1 Like

How can i use a custom key instead of a player to store data?

local BuildStore = DataStore2("BuildData", "server")

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.