How do you use UpdateAsync() correctly?

Hello, I’m new to the forums so sorry if this is the wrong category or something.

So, I recently read this post by @ForeverHD about how you should use UpdateAsync() instead of SetAsync() and I was totally convinced. but, I’m not sure what the darn was going on in some of the code.

Here’s what I think I know about UpdateAsync(). Correct me if I’m wrong:

  • The first parameter of UpdateAsync() is the key of the data value that you are updating.
  • The second parameter is a function that updates the data value with whatever is returned by the function. returning nil cancels the save.
  • The second parameter function thing has it’s own parameter referencing the old value.

Now, what I’m wondering is what is DataId and how could I reference and use it? I know that @ForeverHD was using it to compare the old value with the Players data, but I wanna know how it works.

5 Likes

Yep, your current understanding of UpdateAsync is correct. It is a DataStore method which takes a string as the first parameter, the key to update and a second one, the function to run while updating that key. The DataStore passes the current (old) value of that key to the function so you can reference it.

local function updateData(oldValue)
    local newValue = doSomething(oldValue)

    return newValue
end

DataStore:UpdateAsync("6809102", updateData)

DataId is just part of the example code demonstrating how to use UpdateAsync and is specific to that code sample. It isn’t actually applicable to all code bases. For example, you might have a data system where DataId is a part of player data and you need to reference that. That’s where it comes into play.

local MY_DATA = {
    ["6809102"] = {
        DataId = HttpService:GenerateGUID()
        Data = {Level = 2677}
    }
}

print(MY_DATA["6809102"].DataId)

You don’t need to know anything about DataId in this case. You’re on the right track with your understanding of UpdateAsync and are ready to set to work.

5 Likes

Wdym by dosomething function what you have to do in that function?

doSomething is just an example function. Its purpose is to demonstrate that the transform function passed to UpdateAsync receives a parameter which is the current value held in the DataStore and you can do something using that value.

It’s not important to know what it is to understand the sample. The main parts that you should be looking at are how UpdateAsync works and how you can use it.

  • UpdateAsync accepts a key and a function. The function is passed one parameter which is the current value in the DataStore.

  • You can use this old value if you want to transform data. That’s things like incrementing, working based off of what the value is, table merging and so on. doSomething represents these capabilities, but it is not a specific thing you need.

Assuming the value we previously had in a DataStore is a big string:

DataStore:UpdateAsync("6809102", function(currentString)
    if not currentString then
        currentString = ""
    end

    local newString = currentString .. "some more text"

    return newString
end)
2 Likes