Background
Using :IncrementAsync() to update data stores in all of my games fails to work, causing some player data to not save. This is likely an issue directly with that function, as alternative methods such as :SetAsync() can be a workaround.
Reproduction Steps
Put this code inside a script, then repeatedly join and leave the game:
players = game:GetService("Players")
DS = game:GetService("DataStoreService")
store1, store2 = DS:GetDataStore("Example1"), DS:GetDataStore("Example2")
stores = {store1, store2}
players.PlayerAdded:Connect(function(player)
for _, store in ipairs(stores) do
if not store:GetAsync(player.UserId) then
store:SetAsync(player.UserId, 0)
end
end
print("Times you have left the game (incremented): "..store1:GetAsync(player.UserId))
print("Times you have left the game (set): "..store2:GetAsync(player.UserId))
end)
players.PlayerRemoving:Connect(function(player)
store1:IncrementAsync(player.UserId, 1)
store2:SetAsync(player.UserId, store2:GetAsync(player.UserId) + 1)
end)
Expected Behavior
If a player were to leave three times, and then join the game, the console should output this result:
Times you have left the game (incremented): 3
Times you have left the game (set): 3
Actual Behavior
Instead of the above result for leaving and rejoining a game three times, this is shown instead:
Times you have left the game (incremented): 0
Times you have left the game (set): 3
Issue Area: Engine
Impact: Major
Frequency: Constantly
Date First Experienced: 2021-09-22 16:30:00 (-04:00)