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

Random rejection

  • It does save in-game but sometimes it rejects, which results in non-saving data

Datastore2 seems not maintained anymore, i’m currently too busy to switch to ProfileService
Does anyone know a fix for this error? (DataStore2 > SavingMethods > OrderedBackups)

Edit:

Edited the Standard saving method to retry :point_down: whenever 403 (Forbidden) occurs

self.dataStore:UpdateAsync(self.userId, function()
	return value
end)
Updated Code

Make sure to use the latest Promise release if you use this edit

-- Standard saving of data stores
-- The key you provide to DataStore2 is the name of the store with GetDataStore
-- GetAsync/UpdateAsync are then called based on the user ID
local DataStoreServiceRetriever = require(script.Parent.Parent.DataStoreServiceRetriever)
local Promise = require(script.Parent.Parent.Promise)

local Standard = {}
Standard.__index = Standard

function Standard:Get()
	return Promise.async(function(resolve)
		resolve(self.dataStore:GetAsync(self.userId))
	end)
end

local function updateAsyncRequest(self, value)
	return Promise.new(function(resolve, reject)
		local success, result = pcall(function()
			self.dataStore:UpdateAsync(self.userId, function()
				return value
			end)
		end)

		if (success) then return resolve(result) end
		reject(false)
	end)
end

function Standard:Set(value)
	return Promise.retryWithDelay(updateAsyncRequest, 5, 1, self, value)
end

function Standard.new(dataStore2)
	return setmetatable({
		dataStore = DataStoreServiceRetriever.Get():GetDataStore(dataStore2.Name),
		userId = dataStore2.UserId,
	}, Standard)
end

return Standard

You can literally watch a tutorial on ProfileService that explains it great. One such tutorial is from VectorThree.

i have but i dont use it because i dont understand it even after watching tutorials plus i’d rather make it myself so if i want to add anything i can know where to start

This just looks like a Roblox outage to me.

3 Likes

Yes I know. I wasn’t specific enough. I meant like, isn’t there a way to prevent this from happening, as in retrying to call the function?

I wouldn’t recommend it. Just use the DataStore from Roblox. What I do is I create a module script where, if a player joins, their data will be stored in a table. If they don’t have existing data, new data will be created. I store everything in this table, and when the player leaves, it gets saved.

Suphi’s Datastore Module is the best option rn imo.

1 Like

Well, it has its downsides. I’d still prefer to use ProfileService over Suphi’s Datastore Module.

Thank you so much for creating this, I’m gonna use it for my little game named normal day.
I hope there won’t be any data losess like they happened (4 times they happened and they were pretty big)
if anyone interested what the data losses was so here u go: first and second data loss was the serial keys they were lost 2 times (idk why to this day) the third data loss was the players money and the fourth data loss was companies budget (basically 170k money reset thats alot tho i backed it up to somewhat around this value)

1 Like

These two already explain by DataStore2 should not really be used anymore. You should either use the default Roblox Datastores with their new API and all that stuff or you can use ProfileService which I have given a link to a great video explaining how to use it above.

Edit: Roblox Datastores do provide versioning! (ProfileService does too)

1 Like

why do i get this warn?

Since the points value was not changed/updated, DataStore2 did not save the value.
Edit: Sorry for bumping.

Hey,

is there a Community Ressource for DataStore 2 on Discord or on any other Social Media? If so can you send me the link so I can discuss with someone because I have a lot of question for that

Hey so how do i get the current value of the datastore? Do i just do something like:

local stat = game.Players.bbackstab.leaderstats.Stat.Value

Hey!

I’m not entirely sure if I wrote this correctly: but I’m having the same warning every time I run the game.
I’m trying to save a table of keybinds that each Player can change, so that it will stay the same the next time they join.

Here is the code:

-- VARS ||
local Players = game:GetService("Players")
local Datastore2 = require(script.Parent.DataStore2)
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Data_Sync = ReplicatedStorage.Remotes.DataSync
-- ||

local defaultkeybinds = {
	Interact = Enum.KeyCode.E,
	Console_Interact = Enum.KeyCode.ButtonX,
	SecondaryInteract = Enum.KeyCode.F,
	Console_SecondaryInteract = Enum.KeyCode.ButtonY,
	CloseMenu = Enum.KeyCode.X,
	Console_CloseMenu = Enum.KeyCode.ButtonB
}

Players.PlayerAdded:Connect(function(PLAYER)
	local keybindDataStore = Datastore2("KEYBINDS", PLAYER)
	local keybinds = keybindDataStore:Get(defaultkeybinds)
	
	Data_Sync:FireClient(PLAYER, keybinds)
end)

Data_Sync.OnServerEvent:Connect(function(PLAYER, TABLE)
	local keybindDataStore = Datastore2("KEYBINDS", PLAYER)
	keybindDataStore:Combine("DATA", "KEYBINDS")
	keybindDataStore:Set(TABLE)
end)

I’m specifically having trouble on the last function, it keeps throwing a: error when player left! Invalid at input.Interact because: Invalid type (EnumItem) error whenever I leave the game.

Anyone know the issue and can help me troubleshoot?

I have a question, when I use Get() do I have to pass through the default value every time, I currently use Get() with the defaultValue when the player joins

1 Like

is this module still relevant???

Is there a way to tell when the data is set with the :Set() function? My code has some errors because it continues to run before the new data has been set. For now I just use task.wait(1), but is there a better way to do this? My original post.

it’s easy to use. that’s all that matters. big games still use it, and it’s great for newcomers. don’t see why it shouldn’t be used.

I’m experiencing a frustrating issue where OnUpdate is not returning the newest updated value.

local playerDataTable = {
	playerSwords = thisPlayersDataStores.swordCollectionDataStore,
};

if playerDataTable.playerSwords:Get() == nil then
	playerDataTable.playerSwords:Set({}) -> prints {}
end

playerDataTable.playerSwords:Get()[dreamSwordName] = true -> (does not print anything)
print(playerDataTable.playerSwords:Get()) -> Prints {["Basic Sword"] = true}

I converted the dictionary to an array, however I’m still experiencing the same exact behavior.

  01:59:59.128  RESET  -  Server - PlayerItemFunctions:51
  01:59:59.128  DATASTORE playerSwords SET TO {}  -  Server - Script:174
  01:59:59.128  RESET  -  Server - PlayerItemFunctions:51
  01:59:59.129  DATASTORE playerShields SET TO {}  -  Server - Script:174
  01:59:59.694  playerShields the real value is  ▼  {
                    [1] = "Shield"
                 }  -  Server - Script:178
  01:59:59.694  playerSwords the real value is  ▼  {
                    [1] = "Basic Sword"
                 }  -  Server - Script:178

:man_facepalming: Update: I completely overlooked this issue for 2 hours :clap: the reason was because I was not using datastore:Set(). While the method I was using was correct as it was a metatable, the callback was not being fired.