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

UpdateAsync Broke After Changing Something? [SOLVED]

Asked by 4 years ago
Edited 4 years ago

So, I've been looking for answers all over for days on end. I haven't been able to find anything new for a while now, so I think asking a question here is my last option.

I've changed values around, I've changed the structure of the code, I've changed how the script accesses the values. But with no success. It did work at one point, but I needed to change it and now I can't remember for the life of me what I did.

If anyone can help, it would be greatly appreciated, here is the code:

local dataStoreService = game:GetService("DataStoreService")
local StatStore = dataStoreService:GetDataStore("StatStore")

game.Players.PlayerAdded:Connect(function(player)
    game.Workspace:WaitForChild(player.Name)
    player:WaitForChild("leaderstats")

    local Key = player.UserId

    local Leaderstats = player.leaderstats
    local Steps = Leaderstats.Steps
    local SpeedBoost = player.PlayerGui.Codes.SpeedBoost

    local DefaultData = {
        ["Steps"] = 0,
        ["SpeedBoost"] = false
    }

    local Stats
    local recieved, notRecieved = pcall(function()
        Stats = StatStore:GetAsync(Key) or DefaultData
    end)

    if recieved then
        Steps.Value = Stats
        SpeedBoost.Value = Stats
        print("Stats were recieved")
    else
        StatStore:SetAsync(Key, DefaultData)
        print("Stats were not recieved")
        warn(notRecieved)
    end

    wait(0.1)

    local Stats = {
        ["Steps"] = Leaderstats.Steps.Value,
        ["SpeedBoost"] = player.PlayerGui.Codes.SpeedBoost.Value
    }

    while wait(300) do
        local saved, notSaved = pcall(function()
            StatStore:UpdateAsync(player.UserId, function(oldValue)             
                print("Stats were auto saved!")

                return Stats
            end)
        end)
    end
end)


game.Players.PlayerRemoving:Connect(function(player)
    local User = game.Players:FindFirstChild(player.Name)

    local Leaderstats = User.leaderstats
    local NewSteps = script.Parent.StepLoop.Steps
    local Steps = Leaderstats.Steps
    local SpeedBoost = User.PlayerGui.Codes.SpeedBoost

    local DefaultData = {
        ["Steps"] = 0,
        ["SpeedBoost"] = false
    }

    local Key = User.UserId

    local Stats = {
        ["Steps"] = Leaderstats.Steps.Value,
        ["SpeedBoost"] = User.PlayerGui.Codes.SpeedBoost.Value
    }

    local success, result = pcall(function()
        StatStore:UpdateAsync(Key, function(oldValue)
            return Stats
        end)
    end)

    if success then
        print("Stats were saved")
    else
        print("Stats were not saved")
        warn(result)
    end
end)

(SpeedBoost is a Bool value and Steps is an Int value)

1 answer

Log in to vote
0
Answered by 4 years ago

I eventually figured this out, so for anyone else that might have this problem, make sure the tables are single values.

Example:

--Instead of:
Stats = {
    ["Steps"] = Leaderstats.Steps.Value,
    ["SpeedBoost"] = SpeedBoost.Value
}

--do:
Stats = {
    Leaderstats.Steps.Value,
    SpeedBoost.Value
}

Also, I found that PlayerGui gets removed before the player a lot, it's not at the same time. So just make sure you aren't trying to access values that are in the PlayerGui, an easy way around this is to just change where they're located.

When getting data, do:

Steps.Value = Stats[1]
--Not:
Steps.Value = Stats

The [1] after Stats, is the first value in the Stats table, so for SpeedBoost you would just do Stats[2] because it's the second value in the Stats table.

Ad

Answer this question