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

Ok, I’ve returned and I’ve made some progress on saving/loading tables.
I have figured out saving/loading, kind of. I think there is an issue with Saving, because printing values gives me the same bug.

Heres the script, but the issue is that whenever rejoining, no matter what items were in your inventory, multiples of the 2 same items will appear. Here is the script, I have a feeling its just me being stupid and missing the obvious, but I can’t figure it out.

local players = game:GetService("Players")

local DS2 = require(game.ServerScriptService.DataStore2)

game.Players.PlayerAdded:Connect(function(player)
	wait(3)
	local saveslottimer = Instance.new("NumberValue", player)
	saveslottimer.Name = "SaveSelector"
	saveslottimer.Value = 0
	repeat
		wait()
	until saveslottimer.Value == 1 
	
	local saveslot = player.SaveSlot
	
	
	local stats = Instance.new("Folder", player)
	stats.Name = "Stats"
		
	local money = Instance.new("NumberValue", stats)
	money.Name = "Money"

	local er = Instance.new("NumberValue", stats)
	er.Name = "Eridium"
	
	local level = Instance.new("NumberValue", stats)
	level.Name = "Level"
	
	local exp = Instance.new("NumberValue", stats)
	exp.Name = "Experience"
	
	local maxexp = Instance.new("NumberValue", stats)
	maxexp.Name = "MaxExperience"
	
	local inv = Instance.new("Folder", player)
	inv.Name = "Inventory"
	
	DS2.Combine(saveslot.Value, "Money", "Eridium", "Level", "Experience", "MaxExperience", "Inventory")
	
	local moneystore = DS2("Money", player)
	local erstore = DS2("Eridium", player)
	local levelstore = DS2("Level", player)
	local expstore = DS2("Experience", player)
	local maxexpstore = DS2("MaxExperience", player)
	local invstore = DS2("Inventory", player)
	
	local invholder = {}
	
	money.Value = moneystore:Get(500)
	er.Value = erstore:Get(0)
	level.Value = levelstore:Get(1)
	exp.Value = expstore:Get(0)
	maxexp.Value = maxexpstore:Get(500)
	invholder = invstore:Get()
	
	wait(1)
	
	saveslottimer.Value = 2
	
	money.Changed:Connect(function()
		moneystore:Set(money.Value)
	end)
	er.Changed:Connect(function()
		erstore:Set(er.Value)
	end)
	level.Changed:Connect(function()
		levelstore:Set(level.Value)
	end)
	exp.Changed:Connect(function()
		expstore:Set(exp.Value)
	end)
	maxexp.Changed:Connect(function()
		maxexpstore:Set(maxexp.Value)
	end)
		
	local function SaveInv()
		invholder = {}
		for i,v in pairs(inv:GetChildren()) do
			local realitem = game.ReplicatedStorage.Items[v.Name]
			local required = require(realitem.Info.Information)
			invholder[tostring(required.Id)] = v.Value
		end
		return invholder
	end
	
	inv.ChildAdded:Connect(function()
		SaveInv()
		wait(.5)
		invstore:Set(invholder)
	end)
	inv.ChildRemoved:Connect(function()
		SaveInv()
		wait(.5)
		invstore:Set(invholder)
	end)
		
	wait(1)	
		
	if invholder == nil then
		local item1 = Instance.new("NumberValue")
		item1.Name = "Stone Mine"
		item1.Value = 3
		item1.Parent = inv
		local item2 = Instance.new("NumberValue")
		item2.Name = "Basic Furnace"
		item2.Value = 1
		item2.Parent = inv
		local item3 = Instance.new("NumberValue")
		item3.Name = "Basic Conveyor"
		item3.Value = 10
		item3.Parent = inv
	else
		for i,v in pairs(invholder) do
			for i,a in pairs(game.ReplicatedStorage.Items:GetChildren()) do
				local required = require(game.ReplicatedStorage.Items[a.Name].Info.Information)
				if required.Id == tonumber(i) then
					local item = Instance.new("NumberValue")
					item.Name = a.Name
					item.Value = v
					item.Parent = inv
				end
			end
		end
	end
end)

The bits about ‘required’ is just a module script with info about each item in it. Each item has its own ID, which it saves, as well as the value.

Can I save tools like flash or True values like a bool value

Also does the module have limits

Data:Get(false) --or true
Data:Ser(true) --or false

1 Like

yes, you can save String, Number or Boolean values into a datastore.

(this also includes tables that contain String, Number and/or Boolean Values)

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