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

It is for if you haven’t called Get with a default parameter yet, it’s not always necessary.

1 Like

Is it necessary to combine all keys every time I attempt to index Datastore2?
Or is it that unless I am adding extra keys I should not call Datastore2.Combine again, after already combining keys once in a script ?

The only thing you need to make sure of is that before you call DataStore2("key", player), "key" is combined.

2 Likes

Thank you.

Is this the correct way to save through a function using Datastore 2 ?

function dataFunctions.UpdateStats(player,data, SaveAll )
             if player.leaderstats then 
             local cont = player.leaderstats
             if SaveAll == true then
                   for _,value in ipairs(cont:GetChildren()) do
                   local store = dataStore2(value,player) -- maybe tostring(value)
             store:Save() -- or store:Save(player)??
         end
      else 
           store = dataStore2(data,player)
           store:Save()
   end
      else warn("leaderstats not loaded")                             
 end 


   function dataFunctions.CreateStats(player)

The parameter data would be passed as for example, “coins” so when I run the function it should create a store

datastore2(coins, player)

Where does it explain how to use :Save() and SaveAsync() with Ds2, unless what I’m doing is right?

Will I need to loop constantly after a set interval to save the cached data for every player myself? Or should I only do this by using :bind to close

1 Like

I am unsure of what you are asking, but DataStore2.SaveAll(player) exists.

2 Likes

Does DataStore2 auto-save? If so, Does DataStore2 auto-save when you use ‘Migrate To Latest Update’

2 Likes

@Kampfkarren

Does Datastore2 handle this for you, or should I use SaveAll for a player when they leave as such:

  Players.PlayerRemoving:Connect(function(player)
          datastore2.SaveAll(player)
     end)        

Saving all data for a player when they leave, is this a good idea with Datastore2?

2 Likes

@CleanVac @XxELECTROFUSIONxX

DataStore2 auto saves when players leave.

3 Likes

where and in what case would (and should) SaveAll be used then, if Datastore2 saves data on it’s own?

2 Likes

You can use it for if you want automatic saving, or for Zombie Strike we use it before teleporting players to ensure that the data they get inside the missions is what it should be.

3 Likes

Hi, I’m getting reports of some people’s data not saving though I can’t reproduce the issue. I’ve tracked it down to the function OrderedBackups:Set and it has to do with one of the pcalls returning false. There is no difference between the two, so how would I go about debugging this?

One of the pcalls (the one below) I added RankedInfoATFloors:SetAsync(value.userId, value.totalFloors) to, which updates my leaderboards. This has always worked in the past though so I don’t know why it would break for some people. Take a look below:

local success, problem = pcall(function()
	RankedInfoATFloors:SetAsync(value.userId, value.totalFloors)
	self.dataStore:SetAsync(key, value)
end)

The error I’m getting is on line 386 of the main DataStore2 module with “save error! Argument 1 missing or nil”. I found that this error came from calling the OrderedBackups:Set function I mentioned above.

1 Like

I don’t know what line 386 is if you messed with the code yourself (which by the way, means you have to open source your entire fork of the module). I definitely would not recommend editing the original code to begin with, you’d be better off just using AfterSave and setting the ordered data store then.

1 Like

I didn’t modify the original module, I only added the line to the OrderedBackups saving method that saves the data to the leaderboard (does that count as part of the module?). Also, I’m guessing AfterSave is fired after that part in the saving is completed?

1 Like

@Kampfkarren Is it better to call :Get() for every value saved in a DataStore to update the leaderstats and update the UI showing the value too, using FireClient() on OnUpdate
or
should I only call Get() for updating the leaderboard value and using a .Changed Event to detect the values being changed and then update the UI without calling Get again?

Will too many Get calls used like this cause throttling or something?

1 Like

Yes, it does.

Yes, it does.

1 Like

Get() only uses data store methods the first time you call it. Feel free to call it as much as you want.

1 Like

Thanks to Datastore2, my game had not even a single data loss, without Datastore2 I would have kept loosing data each day, now everything is stable, no longer need to restore data for each player. Datastore2 saved me a lot of troubles. Thank you again for making this awesome creation!

2 Likes

@Kampfkarren

Final question,

And Firing an event for every Client each time every value changes using :Get() is a good idea to update UI containing those values? I know OnUpdate exists, but this is to update stats on the Client, which can’t access the module so I can’t require DataStore2 and get the value using Get(), I fire for every Client OnUpdate each time a value changes, won’t this exhaust some sort of limit for firing for every Client?

In the documentation , there was an example showing how whenever a value changed, using OnUpdate an event was fired for the player using a function CallRemote() to update values on the Client.

2 Likes

No, I use :OnUpdate with remote events and it works fine.

4 Likes

so if I do that for like 20 values it won’t error?

2 Likes