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

Well, I am new to datastores and I have been slaving over for days now trying to save tables and update values in it. Also, I have been scripting on and off for 2 years now and I gotta say the biggest hurdle is not learning how to script, its learning how to script efficiently and have optimized code. I never really took that into consideration so now its like I am screwed. Anyways, thx man for answering

I’m making a slot system, is this an effective way?:

local sss = game:GetService("ServerScriptService")
local DataStore2 = require(sss.MainModule)
local slot = --slotnumber

DataStore2.Combine(
	"DATA", 
	"value1".."Slot"..slot, 
	"value2".."Slot"..slot, 
	"value3".."Slot"..slot, 
)

game.Players.PlayerAdded:connect(function(plr)
	local value1Store = DataStore2("value1".."Slot"..slot, plr)
	local value2Store = DataStore2("value2".."Slot"..slot, plr)
	local value3Store = DataStore2("value3".."Slot"..slot, plr)

    print(value1Store:Get(5))
end)

umm… where is wrong inn my code…i dont think its wrong…can someone tell me where is wrong in my code?..
attempt to index number with ‘Own’

Looks fine, as long as you ensure everything is combined properly.

This isn’t the code that’s erroring, as “Own” is nowhere to be seen.

Hi. First of all thank you for the tutorial :smiley:

I have an optimization question.

How heavy is store:Set(value) on performance? I have a custom inventory system, and there are cases where there might be done store:Set() every second for each player, to update their inventory. Here is the relevant chunk of code

if hit.Name == "GoldMine" and state.Value == "Attacking" then
		
		inventTable["Golddust"] += 1
		inventStore:Set(inventTable)

	end

Will this affect performance too much? And if so, how can i improve this?

Extremely cheap. It doesn’t do any saving on its own.

1 Like

Flummoxed by this error:

Suggesting the value is not a table. The code in question:

local defaultKillCount = {
	Rat = 0,
	Crow = 0,
	Slime = 0
}
local killsStore = DataStore2("Kills", player)
dataKillCount[player] = killsStore:GetTable(defaultKillCount)

That’s it. I ran it once for one player and it worked first time only. I did not update the table from the default values (just checking the code), and now it is completely broken. Is there a workaround to salvage this datastore?

EDIT: must have been a save error in the initial setup. I saved the default table over it manually and now everything works again. I’ll get my save functions up and running!

EDIT2: nope, still broken!

EDIT3: OK, it appears during one of my early tests, I accidentally crossed datastores, filling Kills with “Experience” from Stats. I can no longer overwrite the contents of this datastore. It acts like it overwrites, but then it goes back to this value. The error is because a string is not a table (predictably). Unsure how to clear this datastore out and try again.

1 Like

I love this module, but I’d like to know: How does it manage to not hit the datastore limits??

I’m making a ban system with this datastore and was wondering, is it possible to edit a player’s data that is NOT in the game. I need a way to unban a player (reset ban value to original, which is unbanned) and I don’t know how to do this without them being in the game.

:Save() (or these days, SaveAll) is never necessary. DataStore2 auto saves when the player leaves.

No.

https://kampfkarren.github.io/Roblox/guide/gotchas/

The normal way of doing data stores is to keep a cache somewhere of a player’s data (such as in a folder or using leaderstats), then saving when the player leaves. It is wrong, however, to use DataStore2 this way. DataStore2 is built to be used whenever your data actually changes. You shouldn’t invoke DataStore2 in PlayerRemoving at all. Don’t worry, :Set will never error or do API calls, it’s all cached. DataStore2 will save the player’s data before they leave.

2 Likes

There is no native way with DataStore2 for a plethora of reasons. You can do it manually.

1 Like

Hi, I’m interested in migrating all my players’ data into Datastore2. How do I migrate?

Edit: I pulled an incredibly silly move and forgot to tell you how my datastore worked.
Values are kept in ReplicatedStorage and updated upon player leave.
image
(there are multiple values under statnumbers)
The values are updated upon player leave.
If there’s no way to make Datastore2 values be held (or at least shown) in a folder in ReplicatedStorage, then I will have to redo a lot of code.

DataStore2 isn’t going to randomly use values in ReplicatedStorage, no. You need to write that code yourself.

Im having trouble with data loss. It happens whenever a player got disconnected by internet connection. So i have to save player data every 1 minute. Am i right to do this?

Saving data recurrently is fine, but are you on the most up to date DataStore2 (NOT the free model)?

Very impressive! Just found this. If i understood this correctly, all errors are handled? So i dont need to wrap anything in a pcall right?
Also, nothing needs to be attached to the function before the server shuts down right?

If i just update the data whenever i need to, can i be 100% sure that the data will be saved when the player leaves?

SaveAll has a chance to error, but Get/Set don’t.

No.

Yes.

I made a plugin for this module its still in progress since its my most complex plugin i made. But how it works u just connect to a datastore “Dataname/userid” and press connect button.
On the buttom of the connect screen there is the current datastore key but you can use your desired key like 1,2,3…etc. Once you put the key press enter and you will get a table with everything you stored inside the data key. You can edit or add or remove something from it. Then you can save(every 10 seconds so you dont overload the data). Thanks.
Datastore Link: Datastore2 Editor - Roblox

1 Like

How would I appropriately use SaveAsync() and determine if data sucessfully saved? This is my current snippet of sample code:

InventoryStore:Set(Inv)
local success, errorMessage = pcall(function()
	InventoryStore:SaveAsync()
end)
	if not success then
		print(errorMessage)
	end