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

No, it’s just sugar. It’s equivalent to dataStore:Set(dataStore:Get() + value).

You never HAVE to. Data is saved when the player leaves.

Yes.

Usually if your game can afford to have players load in without their data, it might be a good idea. There is a page on the documentation about this.

Looks fine to me. You should just loop over a list of stat names and create an anonymous function there, though.

The exact same way you save everything else.

1 Like

Wow thanks for the quick answer man!

The exact same way you save everything else.

Well… that’s great😄

You never HAVE to. Data is saved when the player leaves.

When data is saved as soon as the player leaves. Why shall you use :Save() to save data in the current data store to Roblox? Does this have any benefits?

I was wondering if DataStore2.ClearCache() Can be used on a certain player for example if I want to reset their stats or create a new game save?

1 Like

You can use it for auto saves or saving when a player purchases an item with Robux.

3 Likes

ClearCache doesn’t clear their data. Search this thread for “GDPR” to figure out how to reset a players stats, but in that case you should preferably just manually set every key back to normal.

3 Likes

Hey! How well does datastore2 work with games that use roblox’s teleportation service?
I’ve been using datastore2 in my game Tower Defense Simulator, a front page game with 260+ mil visits, since August to fix previous saving issues with my game. Since then dataloss has significantly dropped but we still get reports from players claiming data loss frequently. Recently the issue affected FamedChris’s data in TDS as well.

1 Like

We use DataStore2 in Zombie Strike. We save before teleporting and get no issues.

What’s the player key name for DataStore2? Is it set automatically or does it have to be defined by the developer?

I’m asking these questions so that I can view and edit player data for debugging purposes in my new game.

1 Like

It’s crashing in Studio again … :unamused:
(works in Roblox Player)

image

edit: It’s been patched I think.

1 Like

Quick question, sorry if this has been answered already, But do you need to define

DataStore2.Combine(“DATA”, “KEY”, “KEY”, “KEY”, “KEY”)

In every script or is it only in the for example Game:GetSerivce(“Players”).PlayerAdded event script?

It’s best practice to define every key you use in a script in the Combine, but if you can guarantee it’ll never be retrieved before combining, then it’s fine.

I’m a little lost with the actual saving part. I can get everything to work fine, it even says it saves when I leave the game in print. But it doesn’t actually save.

Print:

player left, saved Test1

local Datastore2 = require(game.ServerScriptService.DataStore2)

local defaultarm = 1
local defaultpic = 1
local defaultgol = 50

game.Players.PlayerAdded:Connect(function(player)
	Datastore2.Combine("Test1", "Gold", "Armor", "Pickaxe")
	--create values
	
	local stats = Instance.new("Folder", player)
	stats.Name = "Stats"
	
	local gold1 = Instance.new("NumberValue", stats)
	gold1.Name = "Gold"
	
	local arm1 = Instance.new("NumberValue", stats)
	arm1.Name = "Armor"
	
	local pic1 = Instance.new("NumberValue", stats)
	pic1.Name = "Pickaxe"
	
	--get stores
	
	local goldstore = Datastore2("Gold", player)
	local armstore = Datastore2("Armor", player)
	local picstore = Datastore2("Pickaxe", player)
	
	local function updategold(gold)
		gold1.Value = gold
	end
	
	local function updatearm(arm)
		arm1.Value = arm
	end
	
	local function updatepic(pic)
		pic1.Value = pic
	end
	
	--load initial data
	
	updategold(goldstore:Get(defaultgol))
	updatearm(armstore:Get(defaultarm))
	updatepic(picstore:Get(defaultpic))
	
	--update data
	
	goldstore:OnUpdate(updategold)
	armstore:OnUpdate(updatearm)
	picstore:OnUpdate(updatepic)	
end)

No errors, it just doesn’t save.

1 Like

you have to have some code changing the value of datastore2 to save the value for example: goldstore:Set(Valor) or goldstore:Increment(Valor)

3 Likes

I already have ways of changing the values, so I’ll have to change them to use Datastore2 I guess

How would I access goldstore from other scripts? Would I just use _G, or could I turn the script into a module script and require it?

Just call DataStore2 again. DataStore2("key", player) returns the same instance every time, as long as the keys and the player are the same.

1 Like

I already do that right here.

	local goldstore = Datastore2("Gold", player)
	local armstore = Datastore2("Armor", player)
	local picstore = Datastore2("Pickaxe", player)
	
	local function updategold(gold)
		gold1.Value = gold
	end
	
	local function updatearm(arm)
		arm1.Value = arm
	end
	
	local function updatepic(pic)
		pic1.Value = pic
	end
	
	--load initial data
	
	updategold(goldstore:Get(defaultgol))
	updatearm(armstore:Get(defaultarm))
	updatepic(picstore:Get(defaultpic))
	
	--update data
	
	goldstore:OnUpdate(updategold)
	armstore:OnUpdate(updatearm)
	picstore:OnUpdate(updatepic)

I tried adding it to the area where it changes the values, and it still doesnt work

1 Like

can you put the change code for DataStore2?

Since short time I get this error over and over again, although I have not changed anything.

It ends up with this:

grafik

Is this my fault or has it something to do with DataStore2?

image
Seem to be getting tons of errors when using GetTable in the newer datastore2 (i wanted to add to the default data)

1 Like
buyup.OnServerInvoke = function(player, art)
	if art == "armor1to2" then
		if player.Stats.Gold.Value >= 50 then
			player.Stats.Armor.Value = 2
			workspace.Buy:Play()
			player.Stats.Gold.Value = player.Stats.Gold.Value - 50
			Datastore2("Gold", player)
			Datastore2("Armor", player)
			player.PlayerGui.Gameplay.Reward.Visible = true
			player.PlayerGui.Gameplay.Reward.Title.Text = "Armor Upgraded!"
			player.PlayerGui.Gameplay.Reward.Reward.Text = "+15 Health!"
			workspace.Relic:Play()
			wait(3)
			player.PlayerGui.Gameplay.Reward.Visible = false
		else
			workspace.Error:Play()
		end
	elseif art == "pickaxe1to2" then
		if player.Stats.Gold.Value >= 100 then
			player.Stats.Pickaxe.Value = 2
			workspace.Buy:Play()
			player.Stats.Gold.Value = player.Stats.Gold.Value - 100
			Datastore2("Gold", player)
			Datastore2("Pickaxe", player)
			player.PlayerGui.Gameplay.Reward.Visible = true
			player.PlayerGui.Gameplay.Reward.Title.Text = "Pickaxe Upgraded!"
			player.PlayerGui.Gameplay.Reward.Reward.Text = "+5 Damage!"
			workspace.Relic:Play()
			wait(3)
			player.PlayerGui.Gameplay.Reward.Visible = false
		else
			workspace.Error:Play()
		end
	end
end