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

I sent a string literal, and got this.

https://gyazo.com/b09d9b7e6c2ad6605ee0d22f8e7236dc


https://gyazo.com/c9ed8bf6050c69e162e3f384eddf90c1

Are you using DataStore2:Combine() instead of DataStore2.Combine()? I believe the first parameter for DataStore2’s __call is the self keyword, which was never used at all there.

1 Like

I just fixed it b4! Thanks tho xD.

1 Like

Yes, the release on GitHub is the most recent.

1 Like

I have a question, when using get if we use

local thingStore = dataStore(“Thing”,player)
thingStore:Get(dontAttemptGet = true)

if thingStore is not in the cache what will it return?

It will return nil.

1 Like

How would I use the dontAttemptGet = true feature?

would it be store:Get( {dontAttemptGet = true})

It would just be store:Get(true), though I feel the need to ask what your use case for this is.

Before the player can interact with the server I have it so once all the players data is loaded it will tell the client to set up their UI’s allowing them to interact with the server.
I have other module scripts that access the cache and the player woudnt be able to interact with them if their data wasnt fully loaded into the cache so i feel there is not need to pass a default value for them. Or would there still be a need to?

You can just prevent the first character spawn until their data loads in. If you call :Get(true) for players when they join, it’ll always return nil because it doesn’t start a cache until you use it once without it.

Oh no sorry I meant that when i do
game.Players.PlayerAdded:Connect
Here i pass the default values

however, lets say in a shop script If i want to check the players balance I would not pass the default value

You don’t need to pass in the default value every time. Checking the cache is free–there’s no real reason to bypass it for this use case.

Sidenote: The signature is actually :Get(nil, true), not :Get(true).

Okay thank you!
I have a couple more questions I made a separate Post because I didnt want to spam this page.

Perhaps I am misunderstanding something in your code and with the cache, but I’m having issues with delayed data updates. If I start in a lobby, go to a level, and go back to the lobby in a somewhat short amount of time (~1-2 minutes), the lobby will seem to still have the cache of my old data the first time I joined the lobby. If I leave and wait long enough my updated data from the level will eventually restore itself. I figure it has something to do with this…

delay(40, function() --Give a long delay for people who haven't figured out the cache :^(
	DataStoreCache[player] = nil
end)

I made my own small function specifically to get rid of any cached values when a player joins the lobby, however it doesn’t seem to help.

function DataStore2.ClearPlayerCache(player)
	DataStoreCache[player.Name] = nil
end

Any suggestions?

Cache is keyed with player instances (which are recreated when a player connects)–not names or user IDs, so whatever is happening is not the fault of DataStore2.

2 Likes

For some reason the data won’t save in my game.

Script of leaderstats:

local Players = game:GetService("Players")
local DataStore2 = require(game.ServerScriptService.Modules.DataStore2)

DataStore2.Combine("DATA","points","wins","nextApplication")

local function OnPlayerAdded(plr)
	if (plr:FindFirstChild("leaderstats")) then return end
	
	local l = Instance.new("Folder")
	l.Name = "leaderstats"
	
	local points,wins = Instance.new("IntValue"),Instance.new("IntValue")
	local pointsStore,winsStore = DataStore2("points",plr),DataStore2("wins",plr)
	
	points.Value = pointsStore:Get(0)
	wins.Value = winsStore:Get(0)
	
	local function updateWins(newWins)
		wins.Value = newWins
	end
	local function updatePoints(newPoints)
		points.Value = newPoints
	end
	
	pointsStore:OnUpdate(updatePoints)
	winsStore:OnUpdate(updateWins)
	
	points.Name = "Points"
	wins.Name = "Wins"
	
	points.Parent = l
	wins.Parent = l
	
	l.Parent = plr
end

for _,plr in pairs(Players:GetPlayers()) do
	OnPlayerAdded(plr)
end

Players.PlayerAdded:Connect(OnPlayerAdded)

DataStore2:

local dataStore2 = require(1936396537)

return dataStore2

Help is appreciated.

EDIT: Just found out the problem was in my awarder module.

1 Like

So whether you combine one large dictionary or not, it won’t affect any throttling?

1 Like

If you use more than one DataStore2 key without combining, you will throttle.

2 Likes

Make sure to follow the tutorial, you’re doing the updateWins and other update functions wrong.

1 Like

Hello, I am seeming to run into trouble using DS2.
I’ve heard its an excellent way to save Boolean values, as well as currencies. So I’ve been trying to do just that. But have seemed to run into some trouble in regards to the UpdateSaving section of my script. (Lines 51 and 56)

As far as my knowledge goes, which is limited of course, I don’t exactly understand this problem. And cannot seem to find a solution myself, or when searched. What exactly am I running into here as an error?

Here is an image of the “OutPut”
https://gyazo.com/be15b2c6e13dd55fbc730e8c7c9fdf84

Here is my ServerScript.

local DataStore2 = require(game.ServerScriptService.MainModule)
local MainKey = "MainKey"
DataStore2.Combine(MainKey, "Stats", "Hats")

local function SetDataTable()
	local UserData = {
		Stats = {
			["Starcoins"] = 0,
			["minplay"] = 0,
		},
		
		Hats = {
			["OrangeHalo"] = false,
			["YellowHalo"] = false,
		},	
	}
	return UserData
end

game.Players.PlayerAdded:Connect(function(plr)
	local UserData = DataStore2(MainKey,plr):Get(SetDataTable())
	
	--// Folders \\--
	local Stats = Instance.new("Folder")
	Stats.Name = "stats"
	local hats = Instance.new("Folder")
	hats.Name = "Hats"
	
	--// Numbers \\--
	local Starcoins = Instance.new("IntValue")
	Starcoins.Name = "Starcoins"
	local minplay = Instance.new("IntValue")
	minplay.Name = "minplay"
	
	--// Bools \\--
	local OrangeHalo = Instance.new("BoolValue")
	OrangeHalo.Name = "OrangeHalo"
	local YellowHalo = Instance.new("BoolValue")
	YellowHalo.Name = "YellowHalo"
	--// End Bools \\--
	
	local StatsData = DataStore2("Stats", plr)
	local HatsData = DataStore2("Hats", plr)
	
	local function UpdateStats(UpdatedValue)
		Starcoins.Value = StatsData:Get(UpdatedValue).Starcoins
		minplay.Value = StatsData:Get(UpdatedValue).minplay
	end
	
	local function UpdateHats(UpdatedValue)
		OrangeHalo.Value = HatsData:Get(UpdatedValue).OrangeHalo
		YellowHalo.Value = HatsData:Get(UpdatedValue).YellowHalo
	end
	
	UpdateStats(UserData.stats)
	UpdateHats(UserData.hats)
	
	StatsData:OnUpdate(UpdateStats)
	HatsData:OnUpdate(UpdateHats)
	
	Stats.Parent = plr
	hats.Parent = plr
	Starcoins.Parent = Stats
	minplay.Parent = Stats
	OrangeHalo.Parent = hats
	YellowHalo.Parent = hats
	
end)
1 Like