Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

UpdateAsync or SetAsync?

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.

1 comment

2 answers

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;

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);

This discussion is preserved as it was. New answers, comments, and votes are closed.