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

Why does this Data Store script not work all the time?

Asked by 8 years ago

This script is in ServerScriptService (not local script). Before I released the game it tested perfectly. After a month and a lot of people playing, I now have some players telling me their leaderstats reset to zero when rejoining the game. I have no clue whats wrong?

Please help, I would be so very thankful.

Thanks so much! Tobycats

01DataStore = game:GetService("DataStoreService"):GetDataStore("StatsService")
02 
03game.Players.PlayerAdded:connect(function(player)
04    repeat wait() until player:FindFirstChild("leaderstats")
05    repeat wait() until player.leaderstats:FindFirstChild("Rank")
06    repeat wait() until player.leaderstats:FindFirstChild("XP")
07    repeat wait() until player.leaderstats:FindFirstChild("Kills")
08 
09    local PlrsRank = DataStore:GetAsync(player.Name .. "_Rank")
10    local PlrsXP = DataStore:GetAsync(player.Name .. "_XP")
11    local PlrsKills = DataStore:GetAsync(player.Name .. "_Kills")
12 
13        if PlrsRank ~= nil then
14        player.leaderstats.Rank.Value = PlrsRank
15        player.leaderstats.XP.Value = PlrsXP
View all 34 lines...

1 answer

Log in to vote
0
Answered by 8 years ago

This is because the :SetAsync() is for immutable variables. For mutable values, values that will always change like statistics, the :UpdateAsync() method is a much better approach since multiple servers may be accessing the data.

Even though you called the method, the actual saving may be done minutes ahead of time due to lag in the server or the request limit.

:UpdateAsync() accounts for the old value of the value being changed, ensuring that it's what it was prior and is not being interrupted/change by anything else during the save. :SetAsync() does not account for this.

0
A general rule of thumb is to use :SetAsync() for immutable variables or data that once set - will never change. :UpdateAsync() is for mutable variables - variables that change frequently or can change. AbstractCode 16 — 8y
Ad

Answer this question