How to save and load the data?

so, I newly started learning about data stores and have a question
why it doesn’t save the player’s data?

local players = game:GetService("Players")
local DataStore = game:GetService("DataStoreService")
local ReplicatedStore = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local DailyRewards = require(ServerScriptService:WaitForChild("DailyRewards"))
local Remotes = ReplicatedStore:WaitForChild("Remotes")
local DDS = DataStore:GetDataStore("Data_29")	

local PlayerRewards = {}
local ClaimedGifts = {}

local function waitForRequestBudget(requestType)
	local currentBudget = DataStore:GetRequestBudgetForRequestType(requestType)
	while currentBudget < 1 do
		currentBudget = DataStore:GetRequestBudgetForRequestType(requestType)
		task.wait(5)
	end
end
players.PlayerAdded:Connect(function(player)
	local leaderstats = Instance.new("Folder",player)
	leaderstats.Name = "leaderstats"

	local coins = Instance.new("IntValue",leaderstats)
	coins.Name = "Coins"
	
	local Values = Instance.new("Folder",player)
	Values.Name = "Values"
	
	local dailyRewardSeconds = Instance.new("IntValue",Values)
	dailyRewardSeconds.Name = "DailyRewardSeconds"
	dailyRewardSeconds.Value = 0
	
	local dailyrewardOS = Instance.new("IntValue",Values)
	dailyrewardOS.Name = "DailyReward"
	dailyrewardOS.Value = 0

	
	
	local success, data = pcall(function()
		return DDS:GetAsync(player.UserId)
	end)
	if success then
		warn("WeGotPlayerAddedTheData")
		if data then	
			warn("PlayerHasData")
			print(data)
			for k, d in pairs(data) do
				if k == "RewardsTable" then
					PlayerRewards[player.UserId] = d	
				else
					if type(d) == "table" then
						for n, v in pairs(d) do
							warn(k,n,v)
							if player[k][n]:IsA("Folder") then -- Is a Folder

							else -- is a Value
								player[k][n].Value = v	
							end
						end
					end
				end
			end
	
			if math.floor(os.time()/86400) > player.Values.DailyReward.Value then
				ClaimedGifts = {}
				player.Values.DailyReward.Value = math.floor(os.time()/86400)
			end
			warn("NotNewPlayer")
			PlayerRewards[player.UserId] = data
		else
			warn("NewPlayer")
			if math.floor(os.time()/86400) > player.Values.DailyReward.Value then
				ClaimedGifts = {}
				player.Values.DailyReward.Value = math.floor(os.time()/86400)
			end
			local CurrentTimestamp = os.time()
			PlayerRewards[player.UserId] = {
				["Reward1"] = CurrentTimestamp,
				["Reward2"] = CurrentTimestamp,
				["Reward3"] = CurrentTimestamp,
				["Reward4"] = CurrentTimestamp,
				["Reward5"] = CurrentTimestamp,
				["Reward6"] = CurrentTimestamp,
				["Reward7"] = CurrentTimestamp,
				["Reward8"] = CurrentTimestamp
			}
		end
	else
		--player:Kick()
		warn("Error somewhere")
	end
end)
	
players.PlayerRemoving:Connect(function(player)
	local userID = player.UserId
	local key = "Player_"..userID
	
	local coins = player.leaderstats.Coins.Value
	local dailyrewardOS = player.Values.DailyReward.Value
	local dailyRewardSeconds = player.Values.DailyRewardSeconds.Value
	local dataTable = {
		["leaderstats"] = {
			Coins = coins,
		},
		
		["Values"] = {
			DailyReward = dailyrewardOS,
			dailyRewardSeconds = dailyRewardSeconds,
		},

		["RewardsTable"] = PlayerRewards[player.UserId],
		["ClaimedGiftsTable"] = ClaimedGifts[player.UserId]
	}
	

	print(dataTable)
	
	local success, data
	print(data)
	repeat
		waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
		success, data = pcall(DDS.UpdateAsync,DDS,key,function()
			return dataTable
		
		end)

	until success
	
	if math.floor(os.time()/86400) > player.Values.DailyReward.Value then
		ClaimedGifts = nil
		player.Values.DailyReward.Value = math.floor(os.time()/86400)
	end
end)

after leaving print(data) returns nil
and at rejoin warn(NewPlayer) line works
so I guess problem is about saving part?
I may not understand well so maybe it is not

Yeah I still wonder the answer.

The problem may be in the players.PlayerRemoving function, where you are attempting to save the player’s data to the DataStore.

In the players.PlayerRemoving function, you are creating a dataTable that contains the player’s data and attempting to save it to the DataStore using the DDS.UpdateAsync method. However, you are not checking the return value of the DDS.UpdateAsync method to see if the data was successfully saved to the DataStore.

1 Like
local success, data
	print(data)
	repeat
		waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
		success, data = pcall(DDS.UpdateAsync,DDS,key,function()
			print(data)
			print(dataTable)
			return dataTable
			
		end)

	until success
 print(data)

the thing is print(dataTable) printed the table I wanted to save,
and the last print(data) prints the values I wanted to save
so I guess right now problem is about loading the data, I believe

if success then 
print(data)

(this is inside of playeradded function)
but data returns as nil at player added.I am really confused about that