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

Realistically you could save anything except threads, userdatas and functions. And the reading/writing logic would be the exact same.

Another question.
Is there a way to manage player’s data if they are not in-game?

i save the players key ID into a separate datastore (not the PlayerData datastore) when a new player joins the game, then you can use the same datastore later to grab all current keys for management purposes, even if the player is offline.

im trying to save a table and load it. its like saves like from bloxburg? (and also my money saving doesnt save or load)

– IF You Can Fix My Scripts I Would Really Appreciate it.

table save code:

local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local ManageSaves = ReplicatedStorage:WaitForChild(“ManageSaves”)
local ds = game:GetService(“DataStoreService”):GetDataStore(“Saves”)
local ds2 = require(game:GetService(“ServerScriptService”):WaitForChild(“DataStore2”))
local Items = ReplicatedStorage:WaitForChild(“Resources”)
local Placed = {}

ManageSaves.OnServerInvoke = function(player,dowat)
if player and dowat then
if dowat == “Load” then
local SavesStore = ds2(“Saves”, player)
if SavesStore:Get() then
print(“a1”)
for i,v in next, SavesStore:Get() do
print(“a2”)
if type(v) == “table” then
print(#v)
print(“a3”)
local ItemToPlace = Items:WaitForChild(v[4]):WaitForChild(v[1]):Clone()
ItemToPlace.Parent = workspace:WaitForChild(“PZone”)
local ItemToPlacePP = ItemToPlace.PrimaryPart
ItemToPlacePP.CFrame = tonumber(v[2])
ItemToPlacePP.Orientation = tonumber(v[3])
end
end
else
warn(“Data Not Found”)
end
end

	if dowat == "Save" then
		print("e1")
		for i,v in next, workspace:WaitForChild("PZone"):GetChildren() do
			print("e2")
			if v:IsA("Model") then
				print("e3")
				local SavesStore = ds2("Saves", player)
				Placed[math.random(1000000,9999999)] = {v.Name,tostring(v.PrimaryPart.CFrame),tostring(v.PrimaryPart.Orientation),v.Category.Value}
				wait(1)
				SavesStore:Increment(Placed)
				--ds:Combine(player.UserId,v.Name,v.PrimaryPart.CFrame,v.PrimaryPart.Orientation,v.Category.Value)
			end
		end
	end
end

end


Money Code:
```
local Players = game:GetService("Players")
local ds2 = require(game:GetService("ServerScriptService"):WaitForChild("DataStore2"))

local DefaultMoney = 1000

Players.PlayerAdded:Connect(function(player)
	game:GetService("ReplicatedStorage"):WaitForChild("PlayMenu"):FireClient(player, true)
	
	local ls = Instance.new("Folder",player)
	ls.Name = "leaderstats"
	
	local Money = Instance.new("IntValue",ls)
	Money.Name = "Money"
	
	local MoneyStore = ds2("Money", player)
	local SavesStore = ds2("Saves", player)
	
	local function UpdateMoney(money)
		Money.Value = money
	end
	
	local function UpdateSaves(savestable)
		local SavesTable1 = savestable -- {stuffinside}
	end
	
	UpdateMoney(MoneyStore:Get(DefaultMoney))
	UpdateSaves(SavesStore:Get())
	
	MoneyStore:OnUpdate(UpdateMoney)
	SavesStore:OnUpdate(UpdateSaves)
end)

Hello,
How we can use DataStore2 to access offline players’ data?
For example, I ban/unban offline player with UserId ingame.

Unfortunately there is none unless you see this topic

Not sure what I am doing wrong but I just wanted to update you from the last time I had dataloss issues. I am using the most recent version of Datastore2 and my datastores are all bind. I have a lot of tables to save which I think could be a cause of this issue?

I recently noticed only some people seem to have dataloss issues. I recently shutdown the server whilst I was still in it and when I rejoined my stats stayed but some other players in the game had reset?

DataStore2 still uses the Roblox data stores so it has the same limits(Data store limits page). The 260k character limit is per key so if you are combining your data, which you should be, the 260k limit is per player as combining your data makes it save under one big table under one key as the players UserId.

2 Likes

you’ll have to save the new players’ keys into a separate datastore, so you can use it to grab players data when theyre offline, you don’t need a plugin to do this.

Im Trying To Save Tables & IntValues But IntValues Dont Save OR Load

But The Tables Save & Load But When I Leave It Forgets It.

Any help?

I would recommend reading the documentation, it seems you are having trouble with basic usage of the module, which is explained there.

2 Likes

could you help me fix my script? its really important

thanks for reading!

  • Also: [ServerScriptService.DataStore2:231: attempt to call a number value]

That means you are treating a number as if it were a function

1 Like

Looks like you passed a number in a function like :OnUpdate or :AfterSave.

Just a couple questions, sorry if they’re simple and were already answered, tried looking around.

With regular DataStores, you’d normally get your DataStore based on a name, then retrieve information through a player designated key. Is this key now the “DATA” used in DataStore.Combine or do we keep a reference to, for the example, coinStore = DataStore2("coins’, player) and using this, retrieve and update the related data?

My final question is on ordered data. Does DataStore2 offer a way to display Leaderboards based on highest values, or are we recommended to maintain our own OrderedDataStore code for this purpose?

For the sake of removing repetition, I ignored some minor questions of mine for now and thanks for any response!

1 Like

Is there a way I can make the BindToClose wait longer before closing? I think it’s closing too quickly and it’s not giving it enough time to save.

On that note maybe something else is causing dataloss because it happens to some players just as they leave and play again another time? I’m not exactly sure what’s wrong

Wow! Ive been looking for something like this for a long time, I’m so glad you made this thread. I can’t wait to give it a shot for myself. :slight_smile:

1 Like

Still Confused :confused: and my game releases in a week

Internally, yes, but you should never use the master key (“DATA” in your case) anywhere outside DataStore2.Combine.

To access stores from multiple scripts, just call DataStore2 again.

You are required to use your own ordered data stores.

1 Like

You can forcefully add a :BindToClose in another script, but it is very unlikely it’s not long enough.

1 Like