Data saving is not working

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 trying to save a player’s currency.
  2. What is the issue? Include screenshots / videos if possible!
    The issue is that the code gets to the second pcall function then stops where the save is supposed to happen(I have comment where it is happening). And the code isn’t giving any errors so I am assuming the code isn’t breaking.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("Player_money")

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local dollar = Instance.new("IntValue")
	dollar.Name = "Dollars"
	dollar.Parent = leaderstats
	
	local player_id = "Player_"..player.UserId
	local data
	local success, errormessage = pcall(function()
		data = PlayerData:GetAsync(player_id)
	end)	
	
	if success then
		dollar.Value = data
		print("data loaded")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local player_id = "Player_"..player.UserId
	local data = player.leaderstats.Dollars.Value
	local success, errormessage = pcall(function()
		print("in pcall")
		PlayerData:SetAsync(player_id, data) -- It gets until here
		print("data saved")
	end)
	print("exited pcall")
	if success then
		print("saved")
	else
		print("not saving")
	end
	
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

While I can not tell you specifically what is wrong or why, I have experienced many issues like this one with the default DataStores.

I really suggest that you migrate to DataStore2 as I have never heard of a case that data saving has broken with it.

4 Likes

I agree with @CAP7A1N, Using DataStore2 is so much more efficient. Might be a little difficult but also pretty simple

Ok. Thank you for the help. I will try that out.

I think it’s saving correctly, but you dont actually do anything once you’ve retrieved the data. You set the gathered data in a variable, but it just stays there.

that’s literally the end of the function. Sure you’ve gathered the data but you need to apply to the game

1 Like

DataStore2 uses regular DataStores, the only thing different is that it handles retrieving, caching and saving data properly for you.

Try retrying when data fails to save


local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("Player_money")
local Players = game:GetService("Players")


local function OnAdded(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local dollar = Instance.new("IntValue")
	dollar.Name = "Dollars"

	
	local player_id = "Player_"..player.UserId
	local data 
	local success, errormessage = pcall(function()
		  data = PlayerData:GetAsync(player_id)
	end)	
	data = data or {["Dollars"] = 0;} -- prevent data being nil
    dollar.Value = data["Dollars"]
    dollar.Parent = leaderstats -- only parent after all changes required have occured

	if not success then
	   warn("there was an error retrieving data!")
	end
end

Players.PlayerAdded:Connect(OnAdded)


local function OnRemoving(player)
      local stats = player.leaderstats:Clone()
	  local player_id = "Player_"..player.UserId
	  local data = stats.Dollars.Value
      local success, retries, errormessage
      
      while not success and retries < 4 do
	    	success, errormessage = pcall(function()
			PlayerData:SetAsync(player_id, {["Dollars"] = data})
		    print("data saved")
            end)
            retries = retries + 1
            wait(4)            
      end
      
	   if not success then
       warn("Error:", errormessage)
   end
end


Players.PlayerRemoving:Connect(OnRemoving)

Use :UpdateAsync() when multiple servers are going to be writing to the same DataStore.

@Mr_bokboy don’t change your system just because it doesn’t work, try improving it.

Sorry if this is reviving a dead post, but I think I’ve figured out the reason. after struggling with the same problem as you, I used some prints and realized the data wasnt actually saving when using SetAsync. That because SetAsync is a yielding function, and PlayerRemoving isnt allowed to yield. Multiple people have gone through with this fine in the past but for some reason this recently started to apply to Datstores as well.