DataStore doesn't seem to save and load

Hello,

I made script which saves player data but for some reason it doesn’t save and load Encoded/Decoded table.
Images:
Player joined to the game.
GetAsync: nil - Data can’t load anything.
Session Data - Default data (Player joined first time)
image

Encoded Data - Session Data when Player leaves
SetAsync - DataStore doesn’t saved Encoded Data
image

here is code:

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local DataStore = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")

local MainData = DataStore:GetDataStore("MainData")

local PlayerData = script.PlayerData

local sessionData = {}

local function loadData(player)
	local PDCopy = PlayerData:Clone()
	local leaderstats = PDCopy.leaderstats
	
	local dataToDecode
	
	local success, err = pcall(function()
		dataToDecode = MainData:GetAsync(player.UserId)
	end)
	
	print("GetAsync:", dataToDecode)
	
	if dataToDecode and success then
		local decodedData = HttpService:JSONDecode(dataToDecode)
		
		sessionData[player.UserId] = decodedData
		
		print(decodedData)
	else
		sessionData[player.UserId] = {}
		
		for i, folder in pairs(PDCopy:GetChildren()) do
			sessionData[player.UserId][folder.Name] = {}

			for tabName, v in pairs(sessionData[player.UserId]) do
				for i, val in pairs(PDCopy:GetDescendants()) do
					if val:IsA("ValueBase") then
						if val.Parent.Name == tabName then
							sessionData[player.UserId][tabName][tostring(val.Name)] = tonumber(val.Value)
						end
					end
				end
			end
		end
	end
	
	print("Session Data:", sessionData)
	
	leaderstats.Parent = player
	PDCopy.Parent = player
end

local function saveData(player)
	local PD = player.PlayerData
	local leaderstats = player.leaderstats
	
	leaderstats.Parent = PD
	
	table.clear(sessionData[player.UserId])
	
	for i, folder in pairs(PD:GetChildren()) do
		sessionData[player.UserId][folder.Name] = {}

		for tabName, v in pairs(sessionData[player.UserId]) do
			for i, val in pairs(PD:GetDescendants()) do
				if val:IsA("ValueBase") then
					if val.Parent.Name == tabName then
						sessionData[player.UserId][tabName][tostring(val.Name)] = tonumber(val.Value)
					end
				end
			end
		end
	end
	
	local EncodedData = HttpService:JSONEncode(sessionData[player.UserId])
	
	print("Encoded Data:", EncodedData)
	
	local sa
	
	local success, err = pcall(function()
		sa = MainData:SetAsync(player.UserId, EncodedData)
	end)
	
	print("SetAsync:", sa)
	
	print("successfuly saved data")
end

Players.PlayerAdded:Connect(loadData)
Players.PlayerRemoving:Connect(saveData)

I didn’t saw similiar problem to me.

Try replacing

sa = MainData:SetAsync(player.UserId, EncodedData)
-- Instead change to
MainData:SetAsync(player.UserId, EncodedData)

And add print(success,err) to see if there’s an error during the saving of the data.

Oh my… I’m dumb, I didn’t got any errors but I forgot to turn on API Services.
I will remember to use success, error instead of variable to get result.

Thanks for the help.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.