Basically this can occure for DataStores:SetAsync, :GetAsync, StarterGui:SetCore, :GetCore. What the problem is that on the time of trying to get the data you are unable to fetch it due to the data loading (this is just an assumption), but this is not the issue you are currently dealing with, but it still can occure so I will discuss this later in the post.
Another issue relating your post is that you are replacing already saved data with other data and receiving the exact same data for both values, this is due to you not saying a table.
for i , v in pairs (saveDataTable) do
myPlayerDataStore:SetAsync(playerUserId, v)
end
Instead just use this:
local key = "Player_"..player.UserId
local saveDataTable = {["D"] = deathsData, ["C"] completionsData} -- D = Deaths, C = Completions
myPlayerDataStore:SetAsync(key, saveDataTable)
Now we will have fix the loading of the data as well, I will use your code for this.
local key = "Player_"..player.UserId
local success, errormessage = pcall(function()
deathsData = myPlayerDataStore:GetAsync(playerUserId)["D"]
completionsData = myPlayerDataStore:GetAsync(playerUserId)["C"]
end)
if success then
deaths.Value = deathsData
completions.Value = completionsData
end
Now using “my” code you have to retry a limited amount of times until it the code actually loads.
I will copy the code from this post
I made.
local globalDataStoreCall do
local MAX_RETRIES = 3
local RunService = game:GetService('RunService')
function globalDataStoreCall(method, key, ...)|
local results = {}
for attempt = 1, MAX_RETRIES do
results = {pcall(myPlayerDataStore[method], myPlayerDataStore, key, ...)}
if results[1] then
break
end
RunService.RenderStepped:Wait()
end
return results
end
end
Now we will call the function every time we want to use :SetAsync or :GetAsync, like so:
globalDataStoreCall("GetAsync", key)
key being "Player_"..player.UserId