This is my first post so go easy on me if i messed the category, thanks!
What im referring to is lets say someone did a trade and left right after the trade ended then proceeded to join a diff server before their inventory was saved properly causing the game to load what they had before the trade but letting the other person keep all the items they received through the trade.
If i were to use something like
LoadedDatas = {}
game.Players.PlayerAdded:Connect(function(Added)
DataStore:UpdateAsync(Added.UserId, function(Value)
LoadedDatas[Added.UserId] = Value
return Value
end)
end)
Wouldn’t it yield till all the other UpdateAsyncs are done writing to the data effectively solving what i described?
What are the downsides to using UpdateAsync instead of GetAsync if there are any?
As far as I know, UpdateAsync is used to save data. Not load data, Although I think DataStore2 is what handles stuff like player leaving and rejoining quickly.
But if a player was to add a value to their leaderstats and leave really quickly im pretty sure it would save.
Hey , it might work but I don’t really get why to use UpdateAsync() on loading, seems inefficient because you are both loading (the function parameter) and setting at the same time.
Not sure, but I think it’s a larger datastore request
It doesn’t seem like you change the data while loading so I think it’s better to just use :GetAsync().
DataStore:UpdateAsync(Added.UserId, function(Value) --Loading the value
LoadedDatas[Added.UserId] = Value --You can do the same with :GetAsync()
return Value -- You haven't changed the value, so you are basically setting the same data.
end)
Here’s how to do it with :GetAsync():
local val --Empty variable to fill data
local success, err = pcall(function()
val = DataStore:GetAsync(Added.UserId) --Loading the value
end)
if success then --Loading worked
LoadedDatas[Added.UserId] = val --Your data
else --Error handling
warn(err)
end
Also suggested to use pcall on UpdateAsync since they both Yield the script.
i’m using UpdateAsync cuz when you use GetAsync before SetAsync is complete it’ll load the version that was saved before and it wont be up-to-date in api reference it says “In cases where another game server updated the key in the short timespan between retrieving the key’s current value and setting the key’s value, UpdateAsync() will call the function again to ensure that no data is overwritten. The function will be called as many times as needed until the data is saved.” so am i miss understanding something
UpdateAsync can also be used for session locking such as if you had a game with economic features such as trading it will prevent duplication of data. It will also prevent overloading to help with data loss.
The main trade off with this is that loading times might take a bit longer than SetAsync / GetAsync, but personally it’s worth it.
im not changing the value the only reason why its UpdateAsync and not GetAsync is cuz UpdateAsync should in theory yield till all the other UpdateAsyncs are done changing the value so in theory no matter what they loaded it will be up to date preventing the vey case i described unless i miss understood?