Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I change my DataStore script from :SetAsync to :UpdateAsync?

Asked by 7 years ago
Edited 7 years ago

I have been trying to figure this out for three days. I have read all about :UpdateAsync but I must be doing something wrong. The first 3 lines are what Im trying instead of :SetAsync. Please help! It would be so very appreciated. Thank you so much!

game.Players.PlayerRemoving:connect(function(player) 
        DataStore:UpdateAsync(player.Name, function(oldValue) return player.leaderstats.Rank.Value end) 
        end)




DataStore = game:GetService("DataStoreService"):GetDataStore("StatsService")

game.Players.PlayerAdded:connect(function(player)
    repeat wait() until player:FindFirstChild("leaderstats") 
    repeat wait() until player.leaderstats:FindFirstChild("Rank") 
    repeat wait() until player.leaderstats:FindFirstChild("XP") 
    repeat wait() until player.leaderstats:FindFirstChild("Kills")

    local PlrsRank = DataStore:GetAsync(player.Name .. "_Rank") 
    local PlrsXP = DataStore:GetAsync(player.Name .. "_XP") 
    local PlrsKills = DataStore:GetAsync(player.Name .. "_Kills") 


     if PlrsRank ~= nil then 
        player.leaderstats.Rank.Value = PlrsRank
        player.leaderstats.XP.Value = PlrsXP 
        player.leaderstats.Kills.Value = PlrsKills
     end
end)

game.Players.PlayerRemoving:connect(function(player) 
      if player:FindFirstChild("leaderstats") then 
        if player.leaderstats:FindFirstChild("Rank") then 
        DataStore:SetAsync(player.Name .. "_Rank", player.leaderstats.Rank.Value) 
        end

     if player.leaderstats:FindFirstChild("XP") then 
        DataStore:SetAsync(player.Name .. "_XP", player.leaderstats.XP.Value)
        end

     if player.leaderstats:FindFirstChild("Kills") then 
        DataStore:SetAsync(player.Name .. "_Kills", player.leaderstats.Kills.Value)
        end
     end
  end)
0
This code doesn't look like it'll benefit from :UpdateAsync. BlueTaslem 18071 — 7y
0
The reason it needs :UpdateAsync is because sometimes the stats rest to zero. Roblox Wiki: If the previous value of the key is important, use UpdateAsync. Using GetAsync to retrieve a value and then setting the key with SetAsync is dangerous because GetAsync sometimes returns cached data, and other game servers may have modified the key. Please help! Tobycats 0 — 7y

Answer this question