Is there a better way to update the player's data (dictionary) without using :Set() on DataStore2?

I’ve just coded this and I wanted to know if there was a better way to update the player’s data (which is a dictionary) without using :Set(). Some friends have told me I shouldn’t use :Set() for updating data when using DataStore2 but I don’t see any functions in the DataStore2 module that could work for me other than :Set().

Yes I know this method to change the player’s cash is stupid and I shouldn’t use it when I officially publish my game.

Method for changing the player’s cash:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Settings = require(ReplicatedStorage.Modules.Settings)

ReplicatedStorage.RemoteEvent.OnServerEvent:Connect(function(Player, Amount)
    local PlayerDataStore = DataStore2("PlayerData", Player)
    
    local function GetDefaultData()
        return Settings.Stats
    end
    
    local DefaultData = GetDefaultData()
    local Data = PlayerDataStore:Get(DefaultData)
    Data.Money = Amount
    PlayerDataStore:Set(Data)
end)

Settings module:

local Settings = {}

Settings.Stats = {
    Money = 200,
    Level = 0,
    MaxLevel = 100,
    XP = 0,
    MaxXP = 5000
}

return Settings
1 Like

No. Whoever told you that you shouldn’t use Set gave you bad advice: the purpose of Set is to update data in the module’s cache. Set does not perform any DataStore calls, it’s simply a way for you to update data quickly and so you should be using it every time you need to update data.

What you shouldn’t be using often is the Save method. Save is what’s used to push the cached data (which is updated with Set) to Roblox DataStores, so obviously at that point you’re bound to the limitations of the service. When it comes to Set though, there’s no limits to how much reading and writing you can or should do.

Always use Set when you need to make a change in player data. Use Save only if you need to flush data to a DataStore after making an important change (e.g. after a purchase), otherwise the module itself will call that function at various points during the game’s lifetime alone.

4 Likes