Data not Saving

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)
  1. What do you want to achieve? I want to efficiently save data, having it not save if there was an error.

  2. What is the issue? The data just isn’t saving, not sure why.

  3. 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.

As a suggestion, you can just do SetAsync when the player is added if there is no current data from the player.

As for your problem, the player’s child (aka, leaderstats) is destroyed and what’s left is the player object with no childrens. You will have to store the data somewhere else (tables, ServerStorage/ReplicatedStorage, etc)

That isn’t the problem. I printed player:FindFirstChild("leaderstats"), and it successfully printed the instance.

1 Like

How about it’s child? Did it also print the instance of Dreams?

1 Like

Yes.

Also, I just noticed from an error I got while printing the instance, that I had made a typo with UpdateAsync(). I actually wrote UpdateAysnc() instead, but even after correcting it, it still didn’t work.

Did you test it in a live game or in studio?

Studio, and I have studio access to API services on.

Maybe try and test it in live game? Also, make sure you are really changing the Value of Dreams.

I am really changing the value, and I tried this in a live game, and somehow, it suddenly worked! I also tried it in studio right after, and it also worked!

1 Like