I know that there are already topics on this, but they seem to be tapping into whether to use SetAsync() or the application of UpdateAsync(). I’m just trying to learn the different functionalities of data store at the moment. I want this data store to save the value of Points.
local pointsDataStore = game:GetService("DataStoreService"):GetDataStore("Points")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local playerKey = "Player_"..plr.UserId
local success, errormessage = pcall(function()
pointsDataStore:UpdateAsync(playerKey, function(oldValue)
local newValue = oldValue or 0
return newValue
end)
end)
end)
Or would this be an instance where SetAsync() would be better? Even after reading posts about that topic, I am still a bit confused as to when you should do either.
LGTM. I fully support your use of UpdateAsync here.
Edit: Ignore my posts. I was under the impression that OP wanted to have the player start with 0 points if they haven’t had anything in the DataStore before.
I think maybe this could be because it’s updating the value before it has time to get the value? Not really sure still. If that were the case, how would I go about waiting for the value?
So the issue would still be that it’s not getting the value from the data store and is instead giving the value of 0 though, wouldn’t it be? When I join the game the value is always 0.
So I tried removing the or 0 part to see what happened and it still doesn’t work. Any feedback would be appreciated.
Current DataStore script:
local pointsDataStore = game:GetService("DataStoreService"):GetDataStore("Points")
local Players = game:GetService("Players")
Players.PlayerAdded:Connect(function(plr)
local playerKey = "Player_"..plr.UserId
local success, errormessage = pcall(function()
pointsDataStore:UpdateAsync(playerKey, function(oldValue)
local newValue = oldValue
return newValue
end)
end)
if not success then
warn("Could not save"..errormessage)
elseif success then
print("Progress updated")
end
end)
The problem with the script is that you are returning oldValue to UpdateAsync. That just sets the new data in the store to the data that is already there. Second issue is that your using UpdateAsync in PlayerAdded. There is no reason to save their data right when they join the game. You probably want to use GetAsync there because it returns the saved data.
To answer the question though, there is a whole article written about it here.
Simplified, it is recommend to use UpdateAsync as it can be used safer, not that it just is safer. Doing UpdateAsync and just returning the new data wouldn’t be much different from SetAsync.
As stated in the article, a good way of utilizing UpdateAsync is by having some kind of DataId that increments by one each time the player saves. The reason this prevents stuff like data loss is because GetAsync may not return the latest data, and if it didn’t, you can check for this inside of UpdateAsync by using the data id - if the data you are about to save is older than the saved data, you can stop saving
I haven’t read the whole thread, but I’m confused.
You’re using UpdateAsync, but you’re just using it to set a default value or keep the existing value? What actual update are you doing here??
Setting a default value is fine and dandy through UpdateAsync, but I don’t know why you’d do that if there’s never going to be anything but a default value.
I’m confused, I just want this to retain the same value that the player has when they leave the game. I.e. If a player leaves the game with 3 points, they should have 3 points when they rejoin.
UpdateAsync updates data. Your updating data when the player joins a game. Use GetAsync if you want to get the amount of points they have saved. Make sure you also use UpdateAsync when the player leaves or there wont be any data to retrieve with GetAsync.