So I tried a different data saving method from the method I normally do, with my new method not saving data if getting the previous data failed. However, My data isn’t saving at all. There shouldn’t be any problem loading the data, just saving. I tried to use a similar method from here (look at the “Writing Data Correctly” section), but I did not do it right.
Here’s the code that loads the data as a value.
--Load stats when player joins
players.PlayerAdded:Connect(function(player)
local stats = Instance.new("Folder")
stats.Name = "leaderstats"
stats.Parent = player
local dreamsValue = Instance.new("IntValue")
dreamsValue.Name = "Dreams"
local success, data = pcall(function()
return dreams:GetAsync(player.UserId)
end)
if success then
dreamsValue.Value = data or 0
else
dreamsValue.Value = 0
end
dreamsValue.Parent = stats
stats = nil
dreamsValue = nil
end)
And here’s the code for saving data when the player leaves, I wrote extra comments to be more clear about what I’m trying to do.
--Save data when player leaves
players.PlayerRemoving:Connect(function(player)
--Check if getting the previous data was successful
local success, data = pcall(function()
return dreams:GetAsync(player.UserId)
end)
--If the data was successfully retrieved, and they have data, update it
if success and data then
dreams:UpdateAsync(player.UserId, function(oldData)
local previousData = oldData or 0
--Check if the player's data is correct
if data == previousData then
previousData = nil
return player:WaitForChild("leaderstats"):WaitForChild("Dreams").Value
else
previousData = nil
return nil --Don't save the data if it isn't correct
end
end)
--If it was successful, but they have no data, set it
elseif success and not data then
pcall(function()
dreams:SetAsync(player.UserId, player:WaitForChild("leaderstats"):WaitForChild("Dreams").Value)
end)
end
end)
-
What do you want to achieve? I want to efficiently save data, having it not save if there was an error.
-
What is the issue? The data just isn’t saving, not sure why.
-
What solutions have you tried so far? I read this post extra carefully, yet I still have no idea why it doesn’t save data.