Any ways to improve my datastore?

Sorry I don’t know how to format my code but here it is.

local DataStoreService = game:GetService(“DataStoreService”)
local DataStore = DataStoreService:GetDataStore(“PlayerData”)

local function LoadData(Player)
local leaderstats = Instance.new(“Folder”)
leaderstats.Name = “leaderstats”
leaderstats.Parent = Player

local Money = Instance.new("IntValue")
Money.Name = "Money"
Money.Parent = leaderstats

local PlayerKey = Player.UserId.."-"

local Success, SavedMoney = pcall(function()
	return DataStore:GetAsync(PlayerKey)
end)

if Success then
	if SavedMoney then
		Money.Value = SavedMoney

	elseif not SavedMoney then
		--//New player
		Money.Value = 250

	end 
end 

end

local function SaveData(Player)
local PlayerKey = Player.UserId…“-”

local Success, Error = pcall(function()
	DataStore:SetAsync(PlayerKey, Player.leaderstats.Money.Value)
end)

if Success then
	print("Data saved")
else
	print(Error) 
end 

end

game.Players.PlayerAdded:Connect(LoadData)
game.Players.PlayerRemoving:Connect(SaveData)

1 Like

I mean one little thing you could do to remove an if statement is this.

-- replace
    if SavedMoney then
		Money.Value = SavedMoney

	elseif not SavedMoney then
		--//New player
		Money.Value = 250

	end 
-- with
Money.Value = SavedMoney or 250

Other than that, instead of using set async, maybe look into update async. And maybe have a system that if the datastore has an error, it tries to fetch the data again

1 Like

What’s the difference between UpdateAsync() and SetAsync()?

So update async you can look at the old value
This could be used to check if the old value is less than the new value.

(When a player purchases something, you should update their datastore cash without the check)

Example: (mobile sorry for typos)

local newVal = 100
Datastore:UpdateAsync(key, function(old)
    if not old or old < newVal then
        return newVal -- update datastores with the new value
    else
        warn("old > new")
        return nil -- don't update the players value
    end
end)

It’s generally safer cause let’s say a player joins, but the datastores fail and they don’t have any cash. But then, the player leaves and it DOES save. Then they lose all their progress and sadness

2 Likes

That’s smart, I’ll definitely look into that function more.

What is the purpose of the newValue variable? Shouldn’t we return their value instead of 100

Save your data in tables, and have a DataID inside your data. You should look at this to understand how to use :UpdateAsync() properly.

1 Like

If anyone sees something incorrect in this, correct me please.


You don’t have any pcalls in case it fails → retry (wait a bit though so that requests don’t go into the queue, you can use the DataStoreService:GetRequestBudgetForRequestType function).
You should implement an autosave so that if there is a DataStore outage, your players won’t lose their entire session.
You should implement corruption handling, Roblox could give you back false values, an errormessage instead of your cash for example → easiest way of doing this is probably with type().

If you ever want to make a trading system, you should have sessionlocking, players can trade away their items, quickly leave and rejoin and they will still have them (the other player aswell), because it took the old server longer to save than the new server to get their value

I recommend using ProfileService or DataStore2, both are great modules that do all this for you (DataStore2 doesn’t have sessionLocking sadly).