Problem with leaderstats script

Hello, can someone tell me where’s the problem in this script? My data saves if I only save one data but if I save two it doesn’t.

local DataStoreService = game:GetService("DataStoreService")
local myPlayerDataStore = DataStoreService:GetOrderedDataStore("myPlayerDataStore")
	
game.Players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = player
	leaderstats.Name = "leaderstats"
	
	local deaths = Instance.new("IntValue")
	deaths.Parent = leaderstats
	deaths.Name = "Deaths"
	
	local completions = Instance.new("IntValue")
	completions.Parent = leaderstats
	completions.Name = "Completion"
	
	local playerUserId = "Player_"..player.UserId
	
	local success, errormessage = pcall(function()
		 deathsData = myPlayerDataStore:GetAsync(playerUserId)
		 completionsData = myPlayerDataStore:GetAsync(playerUserId)
	end)
	if success then
		deaths.Value = deathsData
		completions.Value = completionsData
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local playerUserId = "Player_"..player.UserId
	local deathsData = player.leaderstats.Deaths.Value	
	local completionsData = player.leaderstats.Completion.Value
	local saveDataTable = {deathsData, completionsData}

	for i , v in pairs (saveDataTable) do 
		myPlayerDataStore:SetAsync(playerUserId, v)
	end
end)

the problem is your saving it in a table but when getting it your not

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

2 Likes