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

to save a table you can simply do this:

local defaultTable = {["Points"] = 0}

local mytable = myTableDatastore:GetTable(defaultTable)   --get the table
mytable["Points"] = mytable["Points"] + 1 --edit the table
myTableDatastore:Set(mytable) --set the table (same way as saving an Intvalue)
4 Likes

Hi, for some reason my data doesnt save when the game is shutting down.

I tried doing both:
DataStore(ds.key, player):Save()

and
–SavedFile is a dictionary
DataStore(ds.key, player):Set(SavedFile)

but no luck.

Can you please help me?

Use the latest version on the releases and it should work.

I have a question regarding DataStore:OnUpdate. If I register a callback, it’s called every time the cache is updated. Suppose I make two updates in the cache in quick succession (e.g. I have a table in cache and its values are updated by different scripts. At some point two keys are updated almost at the same time in different scripts). In this scenario, OnUpdate has the guarantee that the first update will finish before the second one starts?

Lua is not multithreaded. It’s impossible for two updates to be called at the same time. Whichever goes first callls OnUpdate.

2 Likes

That’s true. I was confused for a moment because each script runs in its own thread and I wondered if the thread scheduler could switch threads in the middle of the execution of one of the scripts. That’s not the case as explained here (Promises and Why You Should Use Them). Since each update runs until completion or until it yields there is no worries that onUpdate will run over the other.
Thank you for your answer.

Hi,

My game is having problems with saving for mobile users? Could there be any problem with datastore2?

DataStore2 is server sided, and so wouldn’t care about what platform a player is on. I do not have these issues.

1 Like

Is there any way we can message you privately for advice?

You can just PM me on the forums, but I can’t promise I’ll respond quickly.

is it possible to require the module on client and use it as i would on the server?

Datastores don’t work on the client.

DataStore is not supported for LocalScript, however you can get it through remote events!

1 Like

How would I perform rollbacks on data for a game that uses Datastore2?

There is no built in way to do this–DataStore2 containing older data should be seen as an implementation detail, not necessarily as an exposed feature.

1 Like

How would I use :Increment() to update just one value in a table I have saved?

if it is to add a value use this

local DataStore2 = requiere (DataStore2)

DataStore2.combine ("MasterKey", "Money")

local MoneyData = DataStore2 (Player, "Money")

MoneyData:increment (1,0)

--- 1 increment
--- 0 initial value

I know but I want to increment one value that’s in a saved table.

local minerValues = minerSave:Get({
	"Cheap Robo Miners",
	0,
	"Robo Miners",
	0,
	"Buff Robo Miners",
	0,
	"Robo Drill Miners",
	0,
	"Buff Robo Drill Miners",
	0
})

That’s my save and I want to increment just one number at a time by one, so the way I was thinking of doing it was just setting it every time to have the numbers be a physical value’s value and just increment the one I need or to have an on update function for the physical values that updates/sets each value in the table, but it would be much more effecient to just increment it.

This makes dealing with data so much less painful. Thank you so much! :smile:

Please correct me if I’m wrong, but when retrieving values within a table like this, wouldn’t you have to use :GetTable()? This will use the default values in the table if it doesn’t have certain keys instead of simply checking whether or not the table exists like :Get() does.