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

Can Someone help me fully understand the Update Async Datastore Method?

Asked by 3 years ago

I have realized that Update Async is a lot better than Set Async and that it helps get rid of the "DataStore request was added to queue. If request queue fills, further requests will be dropped. Try sending fewer requests.Key = (some random id that I cant find)".

Here is the scripts that i'm working with:

Module script:

local Settings = {
    ["Coins"] = {
        ["parent"] = "Player",
        ["obj"] = "IntValue",
        ["val"]= 0,
    },
    ["DeviceType"] = {
        ["parent"] = "StatsFolder",
        ["obj"] = "StringValue",
        ["val"]= 0,
    },
    ["CoinsMultilier"] = {
        ["parent"] = "Multipliers",
        ["obj"] = "IntValue",
        ["val"]= 1,
    },
    ["Seconds"] = {
        ["parent"] = "TimePlayed",
        ["obj"] = "IntValue",
        ["val"]= 0,
    },
    ["Minutes"] = {
        ["parent"] = "TimePlayed",
        ["obj"] = "IntValue",
        ["val"]= 0,
    },
    ["Hours"] = {
        ["parent"] = "TimePlayed",
        ["obj"] = "IntValue",
        ["val"]= 0,
    },
    ["Donations"] = {
        ["parent"] = "StatsFolder",
        ["obj"] = "IntValue",
        ["val"]= 0,
    },
    ["StagesCompleted"] = {
        ["parent"] = "StatsFolder",
        ["obj"] = "IntValue",
        ["val"]= 0,
    },
    ["TotalCoins"] = {
        ["parent"] = "StatsFolder",
        ["obj"] = "IntValue",
        ["val"]= 0,
    },
}

return Settings

and the Main Leaderstats Script

local data = game:GetService("DataStoreService")
local datastore = data:GetDataStore("Code:000kaq")
local Settings = require(game.ReplicatedStorage.Modules.LeaderstatsSettings)

game.Players.PlayerAdded:Connect(function(plr)
    local prefix = plr.UserId

    local Stat = Instance.new("Folder",plr)
    Stat.Name = "StatsFolder"

    local multipliers = Instance.new("Folder", plr)
    multipliers.Name = "Multipliers"

    local timer = Instance.new("Folder",Stat)
    timer.Name = "TimePlayed"

    for i,v in pairs(Settings) do
        local where = v.parent --Where the datastore gets saved at
        if v.parent ~= "Player" then
            if plr:findFirstChild(v.parent) then--if the folder is already created then
                where = plr[v.parent]
            else
                local folder = Instance.new("Folder",plr) -- creates folder inside player
                folder.Name = v.parent -- sets the folder name

                where = folder -- this was the mistake

            end
        end

        if v.parent == "Player" then
            where = plr
        end
        --// Creates the Value
        local val = Instance.new(v.obj,where)
        val.Name = i -- // The name u did put on left side inside the Module
        val.Value = v.val --// How much if set
    end

    local Data = nil

    local success, err = pcall(function()
        Data = datastore:UpdateAsync(prefix, function(OldData)
            return 
        end)
    end)
end)

The main Script isnt really done yet as you can see at the saving process on the bottom of the code.

I would be super greatful if someone can help me save the data using update Async

0
UpdateAsync updates a datastore if the value returned is not nil. It returns the old data in its second argument, for you to check stuff on it. I dont 100% know how it works either, but it works??? RAFA1608 543 — 3y
0
Theres something about "datastores returning cached content" so maybe it stops it from happening? RAFA1608 543 — 3y

Answer this question