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

How do I use UpdateAsync with tables with bools?

Asked by 5 years ago

How do you use UpdateAsync to update bool values within tables that are going to be saved to datastores? My question is how does UpdateAsync work with tables and how does it know which value you want to be updated?

For example:

local data = {
    car = true,
    truck = false,
    van = false
}

and after you run UpdateAsync() (changes truck and van to be true)

local data = {
    car = true,
    truck = true,
    van = true
}

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

DataStore:UpdateAsync(key, func) is how you'd use it. You pass the key as first argument, but instead of the value you want to update with as second argument, it is instead a function that returns what you want to save. If this function returns nil, or doesn't return a value at all, the update is cancelled. Also note that the function cannot yield, so don't call wait() (or anything else that can yield) in your function!

DataStore:UpdateAsync(key, function(previous)
    return {
        car = true,
        truck = true,
        van = true
    }
end)

previous is a parameter for your function that is just the previous value. If there is already data you can do previous.truck = true, etc


Don't forget to accept if this helps

Ad

Answer this question