DataStore not saving for certain people

We want players to be able to earn money, and then spend that money on different cars, upgrades for those cars and equipment. However, the DataStore is sometimes reverting back to previous data that was stored. Players report buying vehicles and/or upgrades for them, only to have them not save when they leave and join another server. Players have also reported losing money. This issue seems to effect some people more than others. Some players haven’t had any trouble at all, while others frequently lose data. It also appears to be more prevalent in VIP servers.

We have tried using pcalls and BindToClose to help increase the odds of not losing data. We initially thought we fixed the issue after the latest patch, but it was only about an hour before we received more reports of players losing data. At this point it’s becoming very frustrating for not only us, but for our players, and we need to find a solution ASAP.

local dataStore = game:GetService('DataStoreService')
        local playerKey,inventoryKey = 'PlayerBaseData','PlayerInvBaseData'
    local playerStore = dataStore:GetDataStore(playerKey)
local inventoryStore = dataStore:GetDataStore(inventoryKey)

function savePlayerData(player)
local playerDataKey = 'player '..player.UserId..' '..playerKey
local playerInventoryKey = 'player '..player.UserId..' '..inventoryKey
local inventorySaved,playerDataSaved,attempts = false,false,0

    playerDataSaved = pcall(function() playerStore:UpdateAsync(playerDataKey,function() return playerData end) end)
inventorySaved = pcall(function() inventoryStore:UpdateAsync(playerInventoryKey,function() return carData end) end)
end

game.Players.PlayerRemoving:connect(function(player)
    savePlayerData(player)
end)

This isn’t all of the coding, but it’s a chunk of the main coding that handles the saving. Let me know if you need to see more.

1 Like
  1. Please capitilize the c in Connect. The non-capitalized version is deprecated and thus isn’t suggested to be used.
  2. I really suggest to not have a REALLY long Key. The reason states here:
    image

Even the player’s UserId is very sufficient for the key. I doubt a user will have the same UserId of another user.

They won’t; every player has a unique UserId.

In these two lines, you’re using UpdateAsync which will only save data IF there is a returned value. In your code, It doesn’t seem that you’re defining what carData or playerData is.

Keep in mind that if you are defining this somewhere in your code, it doesn’t mean it will work due to the fact that UpdateAsync doesn’t save data when its returned value is nil.

Hope this helps! :wink:

Nothing gets returned as nil from the pcall.