Ad
Still have questions? Join our Discord server and get real time help.
Log in to vote
0

Need some help regarding UpdateAsync()?

Asked by
Soban06 198
4 days ago

So I was using SetAsync() previously. Then, I figured out that it could cause problems while saving the data. So I thought to use UpdateAsync() instead of SetAsync().

My First Question:

Should I add the UpdateAsync() inside the PlayerAdded event or PlayerRemoving? The wiki uses it with PlayerAdded but I want to save the player's data so should it be inside the player removing event? Because that is where the SetAsync() is located.

This was my script before, with SetAsync() (Please note: I am only showing the PlayerRemoving part):

local function save(player)
    local success, errormessage = pcall(function()

        local data = {

            Coins = player.leaderstats.Coins.Value;
            Checkpoint = player.leaderstats.Stage.Value;
            Level = player.leaderstats.Level.Value;
            Rebirth = player.leaderstats.Rebirth.value;

        }

        myDataStore:SetAsync(player.UserId, data)



    end)

    if success then
        print("Data successfully saved!")
    else
        print("There was an error saving the data.")
        warn(errormessage)
    end
end

Now I thought to replace the SetAsync() with GetAsync(). So here is how I tried it:

local function save(player)
    local success, errormessage = pcall(function()

        local data = {

            Coins = player.leaderstats.Coins.Value;
            Checkpoint = player.leaderstats.Stage.Value;
            Level = player.leaderstats.Level.Value;
            Rebirth = player.leaderstats.Rebirth.value;

        }

        -- myDataStore:SetAsync(player.UserId, data)
        myDataStore:UpdateAsync(player.UserId, function(oldValue)
            return data
        end)



    end)

    if success then
        print("Data successfully saved!")
    else
        print("There was an error saving the data.")
        warn(errormessage)
    end
end

Should this do it? Should the thought of player's data not saving properly be removed? Any improvements which I can make?

Please let me know.

Thanks for any help

Answer this question