DataStore2 Loading improvements

Provide an overview of:

  • What does the code do and what are you not satisfied with?
    So basically I’m using DataStore2 to implement a Money System. Players join the server (first time) and get 100 “Currency”. They can get more money for time they spend online on the server. All of this works fine and everything gets saved and comes back after someone leaves and rejoins. The point where I’m not satisfied with is, that it looks like that DataStore2 got a lot of Data loading fails (or at least only in studio?) It happens most of the times, that the money does not get loaded.
  • What potential improvements have you considered?
    I tried with adding lua currencyStore:SetBackup(5) to prevent loading fails, but it seems not to work or at least I did it wrong.
local players = game:GetService("Players")
local replicatedStorage = game:GetService("ReplicatedStorage")

local DataStore2 = require(1936396537)

local defaultCurrencyAmount = 100

local giveDelay = 600
local giveReward = 5
-- Updates Money changes  --

players.PlayerAdded:Connect(function(plr)
	local currencyStore = DataStore2("currency", plr)
		currencyStore:SetBackup(5)
	
	local function updateClientCurrency(amount)
		replicatedStorage.RemoteEvents.UpdateClientCurrency:FireClient(plr, amount)
	end
	
	updateClientCurrency(currencyStore:Get(defaultCurrencyAmount))
	
	currencyStore:OnUpdate(updateClientCurrency)
end)
	-- Money for Time spending online --
while wait(giveDelay) do
	for k, v in pairs(players:GetChildren()) do
		local currencyStore = DataStore2("currency", v)
		
		currencyStore:Increment(giveReward)
	end
	wait(1)
end

Thank y’all already for your help. If you find anything else I can change, feel free to tell me.

Right off the bat we have an issue. Kampfkarren says he doesn’t update the free model due to a Roblox bug, so the module you are requiring is quite outdated.

All documentation and where to get the latest version of DataStore2 is in here: How to use DataStore2 - Data Store caching and data loss prevention

1 Like

Oh thank you, I will look into it. I didn’t know that the module is outdated and not getting updated.