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

This is not correct. Your code probably looks like:

Players.PlayerAdded:connect(function(player)
    local data = DataStore2("key", player):Get()
    player.CharacterAdded:connect(function(character)
        -- Uh oh! :Get() had to yield, so this isn't run until the character respawns!
    end)
end)

Just connect CharacterAdded before using :Get() or use :Get() in CharacterAdded itself.

1 Like

Berezaa I also want to add what you said about having a datastore then a ordered one but another thing beginning scripters are failing to understand is when you make data store scripts you can’t just say DataStore:SetAsync() you have to use UpdateAsync() first then you use SetAsync() or else the datastore will probably be saving the wrong value or even your old one just wanted to put that out also you are very smart I was even thinking about doing that to but I never had the courage until you did it :smiley:

1 Like

Need some help with datastore2. I am trying to figure out how this works and I am so confused. So to get the data, I have to do

local datastore = DataStoreService:GetDataStore(dataStoreName.."/"..player.UserId).
But then how do I get the data with GetAsync?
data = datastore:GetAsync(?)

I released my game using this and I need some help because data is throttling. What causes it to throttle so much? Get? Set? Thats really all I use.

Why are you asking me, that’s not DataStore2 code?

local dataStoreKey = dataStoreName .. "/" .. player.UserId

DataStoreService:GetDataStore(dataStoreKey)

That code is from the module itself. I am wondering how I would get the data from the datastore that your module created. It seems like I would do

datastore = DataStoreService:GetDataStore(dataStoreName.."/"..player.UserId) to get the datastore but I don’t know what I need to put in dataStore:GetAsync to actually get the players data.

I released my game with datastore2, but its throttling a lot and I am trying to get new data store system in without them losing their data.

It seems like it’d be better to figure out the throttling issues than to do this. DataStore2 is built to prevent as many throttling issues as possible. Are you using many unique stores and didn’t follow what the thread said about combined data stores?

Either way, I wouldn’t get it directly–I would just use the module and get the data from there to move over to another store.

I will explain how I did it. New Data is a dictionary of data the new player gets. Data_Type is a string called “Data_Type_2” which is just the name of the datastore I guess.

game.Players.PlayerAdded:Connect(function(player)
	--Fetch Data
	local PlayersData = DataStore2(Data_Type,player)
	PlayersData:SetBackup(3)
	
	local playerData = PlayersData:Get(NewData)

function getData(player)
     return DataStore2(Data_Type,player):Get(NewData)
end

function setData(playerData,player)
     DataStore2(Data_Type,player):Set(playerData)
end

When the player joins I set their data up, and if they have no data I give them the new data. Anytime I need to access the players data, I use the function getData. After I manipulate their data I do the function setData. The only time I save it is if the player purchases something.

My game has about 20-30 players, and I am having trouble with it. The data system I had before didn’t have problems. When a player plays a match, it gives them rewards. I do not do save() when the player exits. Part of the dictionary seems to save (the players XP.) but the diamonds don’t seem to. However when I rejoin the game, sometimes it looks like it saved. I am so confused on whats happening. Help meh

1 Like

How many unique data stores are you using, though? What is “Data_Type”? Every time a new player joins you get more requests to work with, so I doubt that it’s the module’s fault.

Data_Type = "Data_Type_2"

This is the only unique data store I am using so I don’t know what’s going on… Does it matter that I am saving a dictionary instead of an integer or whatever?

I’m not sure with this little code, but DataStore2 should work with that many players–:Get() is only called the first time you use it (and making new DataStore2 objects won’t affect this).

This makes no sense as a DataStore2 issue considering DataStore2 doesn’t save dictionaries differently–this is completely a “your code” issue.

Hello, Seems like OnUpdate doesn’t work for me. Everything is in playerAdded, but Seems like OnUpdate is never fired. It should fire because I do Update coinStore in separate script

-- Update GUI of Coin
function SendToPlayer(Coin, FirstTime)
    print("Fire")
	ReplicatedSorage.DataRemotes.UpdateCoinGUI:FireClient(Plr, Coin, FirstTime)	
		
end


SendToPlayer(CoinStore:Get(20000), true)

CoinStore:OnUpdate(SendToPlayer(CoinStore:Get(20000), false))

This line is wrong.

CodeStore:OnUpdate(function()
    SendToPlayer(CoinStore:Get(20000), false))
end)

Then it’s also wrong in your example code, would be better if you changed it, for such things to never happen again. Thank you very much, now that I understand the module better I acknowledge how better and easy it made my coding experience and game development in general

So it should work no matter how many times I use Get() and Set()?

Nothing is wrong with the example.
OnUpdate is a pseudo event therefore it takes a callback to a function as it’s parameter.

You were just calling a function inside the parameter which would pass the return values to OnUpdate.

The change he made to your code was to wrap your code in an anonymous function and passing that function(unexecuted - no parenthesis after) into OnUpdate. This is an important distinction that is used many places.

Could also be this if it helps you visualize it.

local function anon()
    SendToPlayer(CoinStore:Get(20000), false))
end
CodeStore:OnUpdate(anon)
7 Likes

Yes. :Get() only makes a data store request the first time you use it, and :Set() never makes a request at all.

2 Likes

I have a question. How would I clear data for a specific person? I know to clear all data I would just needs to change the keys (unless I’m wrong about that too), but I don’t know how I would clear just one player’s data.

1 Like

If they’re in game, you can just :Set(nil). If they’re not, the easiest way is just to log their name somewhere and let the game know to wipe their data.

If you’re doing this for GDPR, I already have a command line script that works:

2 Likes

Thanks, my concern was for GDPR. I’ll be sure to use your command line script.

1 Like

Does the module automatically take care of server crashes? I kept using the developer shutdown tool on my scripting test place but it didn’t save.

2 Likes