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

UpdateAsync or SetAsync?

Asked by 8 years ago

In my games I have multiple cases of data loss for no reason using datastores. I am using getasync and setasync, it has never happened to be but for other people. I have it setasynce data every minute for auto saving. Should I use update async?, if so please help me with that because I dont understand the wiki. Please help.

2 answers

Log in to vote
0
Answered by 8 years ago

The difference is negligible unless you are likely to be setting the same values in another server. Your data loss is likely to be caused by how and when you're setting your keys, and if it's on PlayerRemoving you'll need to set game.OnClose.

game.OnClose = function() wait(10) end;
Ad
Log in to vote
0
Answered by 8 years ago

SetAsync sets the key, overwriting the value. That is, whatever the key was set to gets removed, replaced by the new value you're setting. The problem here is that DataStores aren't instant, so the delay means you might set the value again and again, without getting the older one first.

To combat this, switch from using SetAsync

 DataStore:SetAsync(key, value);

to using UpdateAsync

DataStore:UpdateAsync(key, function(oldValue)
    local newValue;
    -- do something with the old value here
    -- maybe: newValue = oldValue + 123;
    -- return the updated value
    return newValue;
end);

Answer this question