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

No, it is not needed. The data is saved everytime it is updated

no the scripts saves when people leaves

Hello, I am not too sure about this but can I have different places within the same game but I do not include some of the data? Not too good at explaining this but I give you an example

--Start Place
DataStore2.Combine("Data", "Cash", "Stats1", "Stats2")

--Place 2
DataStore2.Combine("Data", "Cash", "Stats1")

--Place 3
DataStore2.Combine("Data", "Cash", "Stats2")

So is this possible?

1 Like

That works out fine, yeah.

3 Likes

I have one more question. Is there any difference between saving by UserId and saving by Player? Like if you save by Player, and the player changes their Username, will their data still be the same?

People prefer to use a players userid. If you use their username, you risk them changing it and it will be removed.

Basically, Userid: Works either way if the person has switched username.

I meant as in save by Player and not Username

Edit: By player I meant like when you do a

game.Players.PlayerAdded:Connect(function(player)

and you get that player

1 Like

I don’t know what you mean by “save by player”–you don’t need to think about user IDs at all when using DataStore2.

2 Likes

So if a save using the method you provided in the example at GitHub and the player changes their name, they would not have any data loss right?

DataStore2 its best module ever, but i have a HUGE problem. Everytime when im using/editing data and leaving from Play Mode in studio. My studio crashes with error like.
“Script that called a callback was destroyed”.
No useful info in “More Info”. So i just need keep restarting my studio to test scripts. ;C.

Try create an autosave script.

I just realised there’s a :GetTable, however I’ve been using :Get with tables as I thought this was the only way. Is there an issue if I were to use :Get for tables instead of :GetTable? Is there any disadvantages to this?

No, not much. You can still use :Get() and it should work fine.

1 Like

Here is part of the DataStore2 api about GetTable

DataStore:GetTable

GetTable(default: Dictionary<any, any>) → Dictionary<any, any>
Will get the value (either from the cache or Roblox data stores), and patch it with the default value if it doesn’t have the keys. For example, if you have the data:

{
coins = 0,
swords = {},
}
…and want to add a gems field, just appending on that to the default value for Get won’t work the way you might want it too–it won’t add the key!

– Oops! The player already has data, so it doesn’t try to use the default value!
dataStore:Get({
coins = 0,
gems = 0,
swords = {},
})
You can, however, use GetTable:

– Much better! It’ll check if they have each key rather than just if they have data.
dataStore:GetTable({
coins = 0,
gems = 0,
swords = {},
})
Note

This is not necessary to use tables with DataStore2. You can save/retrieve tables just like any other piece of data.

2 Likes

How do I save bools and strings? I wanna know what I use is it Increment?

Please read the documentation. These are extremely basic questions answered in detail.

2 Likes

I can’t find anything about bools or strings??? Where is it.

1 Like

Why i get this error? I know it appears when i request too many DataStore Requests, but…

Screenshot_311

It happens here:

DataService:Increment(player, "Gold", reward)
DataService:Increment(player, "TotalGold", reward)
DataService:Increment(player, "TotalKills", 1)

Also later (like second after thing above) i do:

DataService:Set(player, "Items", newtable)

How this functions looks like inside DataService Module:

function DataService:Set(plr, store, value)
    local success, result = pcall(function()
    	DataStore2(store, plr):Set(value)
    end)

	if not success then
		warn(result)
	end
end`


function DataService:Increment(plr, store, value)
	local success, result = pcall(function()
		DataStore2(store, plr):Increment(value)
	end)

	if not success then
		warn(result)
	end
end

When player logins i get his data (so it should be inside cached table inside DS2), and so after i change it like set new data, or add numbers, this isn’t supposed to do DataStore calls and so no yield, but i get this yield. Why is this happening?

you can use :Set to save booleans, strings, tables, anything…

DataStore:Set

DataStore:Set(newValue: any)

Will set the cached value in the data store to newValue . Does not make any data store calls, and so will never yield.

1 Like

That would be because you’re not combining your keys, most likely.

Also, Increment/Set can never fail, you don’t need to pcall them.

2 Likes