How to update data but only if it's smaller than the previous data?

Say I want the game to update someone’s data when they leave, but only if it’s larger (better) than the data they previously had. How would I go about doing this?

I hardly ever use datastores because of their complexity. It’s just hard to wrap my head around that stuff so any answers would very much help.

Assuming you already know how to do simple DataStore-stuff (saving and loading), and assuming the data you loaded is changing and doesn’t stay the same, what you can do is create multiple IntValues etc. You can name them to “Saved{data name}” and whenever a player leaves, check if Saved{data name}.Value > Current{data name}.Value.

game.Players.PlayerAdded:Connect(function(plr)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = ""
    leaderstats.Parent = plr
   
    local cash = Instance.new("IntValue")
    cash.Name = "Cash"
    cash.Parent = leaderstats

    local savedCash = Instance.new("IntValue")
    savedCash.Name = "SavedCash"
    savedCash.Parent = plr -- if you want, you can create another folder to store these values


    local data
    -- your loading mechanism bla bla

    if data then
        cash.Value = data -- the cash IntValue can change
        savedCash.Value = data -- this one you wont change
    else
        cash.Value = 0
        savedCash.Value = 0 -- they never were in the game
    end
end)

game.Players.PlayerRemoving:Connect(function(plr)
    local playerData = plr.leaderstats.Cash.Value
    local oldData = plr.leaderstats.SavedCash.Value
    
    if playerData.Value > oldData.Value then
        -- save new value
    end 
end)

You could also use a table in this script if you don’t wanna handle 2 IntValues.

local oldDataOfUsers = {}

-- player joins
oldDataOfUsers[plr.UserId] = data

-- player leaves
if plr.leaderstats.Cash.Value > oldDataOfUsers[plr.UserId] then
    -- save
end

If you know absolutely 0 data stores at all, you can check out some tutorials.

If you have multiple values, just repeat the same with all the other values.

1 Like

I’m sorry but can you elaborate on this?

game.Players.PlayerRemoving:Connect(function(Player)
if Player.leaderstats.Cash.Value > DataStore:GetAsync(Player.UserId.."-key") then -- GetAsync is an Asynchronous function, should be wrapped in a pcall
DataStore:SetAsync(Player.UserId.."-key", Player.leaderstats.Cash.Value)
-- Do stuff here
else
print(Player.Name.."'s stats are not valuable enough!")
end)

By “your loading mechanism”, I mean whatever you use to load data. Do you not know anything about data stores at all? If so, I’d recommend you check out the tutorials.

Laziest way is just to mark a variable in the player if you ever needed to increment their data. Typically you should be saving in all cases anyway but I’m not sure of your use case and why data should only save if it’s better/larger rather than in all cases though - would help to know.

Some examples of what this might look like. These are not full code samples, it’s closer to pseudocode at best. You’ll have to come up with your own code and systems if you like this idea.

-- Example of your data table here:
local data = {
    [6809102] = {
        WasIncremented = false,
        foobar = 0,
        -- other data entries here
    }
}

-- In some update function or as a single line:
local playerData = data[6809102]
playerData.WasIncremented = true
playerData.foobar += 5 -- or whatever

-- In some save function:
local playerData = data[6809102]
if playerData.WasIncremented then
    playerData.WasIncremented = nil -- remove from data table
    saveToDataStore(playerData, 6809102) -- save function or whatever
end
2 Likes

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)

Sorry if there are typing errors lol.

That will check if the player’s old value is bigger than it’s current new value. If the old is bigger then it won’t update the players value.

1 Like

Try this:

local DSS = game:GetService("DataStoreService")
local DS = DSS:GetDataStore("updatestore")

game.Players.PlayerAdded:Connect(function(player)
  local points = Instance.new("IntValue", player)
  points.Name = "Points"

  local data
  local success, err = pcall(function()
     data = DS:GetAsync(player.UserId.."-points")
  end)

  if success then
    points.Value = data
  else
    print(err)
  end
end)

game.Players.PlayerRemoving:Connect(function(player)
  local data
  local success, err = pcall(function()
     data = DS:SetAsync(player.UserId.."-points", player.Points.Value)
  end)

  if success then
    print("successfully saved data!")
  else
    print(err)
  end
end)
1 Like