You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I am attempting to make my daily chest system save the current time.
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)
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)
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