How to fix my data store for my daily chest?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

I am attempting to make my daily chest system save the current time.

  1. What is the issue? Include screenshots / videos if possible!

Its not saving and I get no errors. I check my print statements in the removing event and it prints out with the newest time but when I use the added event ir prints out old data that is not updated. Yes studio access to data store is on.

my code:

game.Players.PlayerAdded:Connect(function(player)
	local key = player.UserId.."-UserId"

	local data = RewardStore:GetAsync(key) or {DailyReward = {os.time(), 201}, GroupReward = {os.time(), 201}}
	print(data.DailyReward[1])
	print(data.DailyReward[2])

	player:WaitForChild("HiddenStats").DailyRewards.Value = HttpService:JSONEncode(data)

	for _,v in pairs(DailyGroupReward:GetChildren()) do

		local claimTimes = {}
		local lastLogins = {}
			
		lastLogins[v.Name] = os.time() - data[v.Name][1] 
		claimTimes[v.Name] = data[v.Name][2]

		claimTimes[v.Name] = lastLogins[v.Name] + claimTimes[v.Name]

		spawn(function()
			while wait(1) do
				for _,v in pairs(DailyGroupReward:GetChildren()) do
					if player[v.ValueName.Value].Value == false and claimTimes[v.Name] ~= nil then
						claimTimes[v.Name] = claimTimes[v.Name] + 1
		
						if claimTimes[v.Name] >= WAIT_TIME then
							player[v.ValueName.Value].Value = true
						end
					else
						claimTimes[v.Name] = 0
					end
				
					local oldTable = HttpService:JSONDecode(player:WaitForChild("HiddenStats").DailyRewards.Value)
					oldTable[v.Name][2] = claimTimes[v.Name]
					player:WaitForChild("HiddenStats").DailyRewards.Value = HttpService:JSONEncode(oldTable)
				end
			end
		end)
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local key = player.UserId.."-UserId"
	local oldTable = HttpService:JSONDecode(player:WaitForChild("HiddenStats").DailyRewards.Value)

	pcall(function()
		print(oldTable.DailyReward[2])
		print(os.time())
		RewardStore:SetAsync(key, {DailyReward = {os.time(), oldTable.DailyReward[2]}, GroupReward = {os.time(),  oldTable.GroupReward[2]}})
	end)
end)
1 Like

Instead of saying os.time in set async get the os.time using a variable and say it there? Sorry I’m not the best scripter but pretty good!

I can try but that seems useless as it is the fact that the data is not saving and not the os.time()

Instead of saying while wait do use while true do and under that say wait(1)

um…that just changes the loop. The issue is with datastore and not loops

Instead of using spawn use pcall? Or xpcall?

ok dude you are really just talking about random stuff, please dont guess. If you dont know, its fine

1 Like

pcall returns two values. The first is a boolean indicating whether the code inside ran successfully, and the second is the value returned by the enclosed function (nothing in this case) if the call was successful, or an error message if the call was unsuccessful.

This is not necessarily a fix, but it could help you to better identify the problem by confirming whether or not the issue is residing in the SetAsync call.

game.Players.PlayerRemoving:Connect(function(player)
	local key = player.UserId.."-UserId"
	local oldTable = HttpService:JSONDecode(player:WaitForChild("HiddenStats").DailyRewards.Value)

	local success, message = pcall(function()
		print(oldTable.DailyReward[2])
		print(os.time())
		RewardStore:SetAsync(key, {DailyReward = {os.time(), oldTable.DailyReward[2]}, GroupReward = {os.time(),  oldTable.GroupReward[2]}})
	end)
	if (success) then
		print("Data saved.")
	else
		print("An error occurred with saving data:")
		warn(message)
	end
end)

so after using a pcall it does not print data saved and it doesn print an erro occured

If nothing is being printed at all then that must mean the PlayerRemoving event isn’t being fired.

thats what I thought too but once a put a print statement after the removing it prints fine but the pcall still doesnt work

Hmm maybe it’s not reaching the pcall block. There’s a call to WaitForChild that might be causing the code to yield. Maybe update it to this and see what happens:

game.Players.PlayerRemoving:Connect(function(player)
	print(player, "has left the server.")
	local key = player.UserId.."-UserId"
	local stats = player:FindFirstChild("HiddenStats")
	print("HiddenStats exists:", (stats ~= nil))
	local oldTable = HttpService:JSONDecode(stats.DailyRewards.Value)

	local success, message = pcall(function()
		print(oldTable.DailyReward[2])
		print(os.time())
		RewardStore:SetAsync(key, {DailyReward = {os.time(), oldTable.DailyReward[2]}, GroupReward = {os.time(),  oldTable.GroupReward[2]}})
	end)
	if (success) then
		print("Data saved.")
	else
		print("An error occurred with saving data:")
		warn(message)
	end
end)

Since this function is being called when the player is leaving i’ll assume that we don’t have to wait for the stats folder to appear in the player since they would have already been in the game for some time. Hence the replacement of WaitForChild with FindFirstChild