local defaultTable = {["Points"] = 0}
local mytable = myTableDatastore:GetTable(defaultTable) --get the table
mytable["Points"] = mytable["Points"] + 1 --edit the table
myTableDatastore:Set(mytable) --set the table (same way as saving an Intvalue)
I have a question regarding DataStore:OnUpdate. If I register a callback, it’s called every time the cache is updated. Suppose I make two updates in the cache in quick succession (e.g. I have a table in cache and its values are updated by different scripts. At some point two keys are updated almost at the same time in different scripts). In this scenario, OnUpdate has the guarantee that the first update will finish before the second one starts?
That’s true. I was confused for a moment because each script runs in its own thread and I wondered if the thread scheduler could switch threads in the middle of the execution of one of the scripts. That’s not the case as explained here (Promises and Why You Should Use Them). Since each update runs until completion or until it yields there is no worries that onUpdate will run over the other.
Thank you for your answer.
There is no built in way to do this–DataStore2 containing older data should be seen as an implementation detail, not necessarily as an exposed feature.
That’s my save and I want to increment just one number at a time by one, so the way I was thinking of doing it was just setting it every time to have the numbers be a physical value’s value and just increment the one I need or to have an on update function for the physical values that updates/sets each value in the table, but it would be much more effecient to just increment it.
Please correct me if I’m wrong, but when retrieving values within a table like this, wouldn’t you have to use :GetTable()? This will use the default values in the table if it doesn’t have certain keys instead of simply checking whether or not the table exists like :Get() does.