Datastore not saving data. Help!

  1. What do you want to achieve? Keep it simple and clear!
    I want to save data to and autosave slot for my game.

  2. What is the issue? Include screenshots / videos if possible!
    When I :SetAsync, it does not update the datastore.

Data that gets saved:
image

Data when loaded:
image

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    No solutions found.

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!

Before you ask:

  • Yes, I checked if it’s using the correct player ID
  • I have API services on

Problematic code:

local function PlayerAdded(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	
	local dollars = Instance.new("IntValue")
	dollars.Name = "Dollars"
	dollars.Parent = leaderstats
	
	local rp = Instance.new("IntValue")
	rp.Name = "RP"
	rp.Parent = leaderstats
	
	leaderstats.Parent = player
	
	local success = nil
	local playerData = nil
	local attempt = 1

	repeat
		success, playerData = pcall(function()
			local data
			data = database:GetAsync(player.UserId)
			return data
		end)
		attempt += 1
		if not success then
			warn(playerData)
			task.wait(3)
		end
	until success or attempt == 5

	if success then
		print("Connected to database", playerData)
		
		if not playerData then
			print("Assigning default data")
			playerData = {
				["Dollars"] = 10000000,
				["RP"] = 0,
				["ShipSaves"] = {}
			}
		elseif playerData.ShipSaves.AutoSave then
			
			print("Loading autosave data")
			
			local plot_folder
			
			repeat plot_folder = MS.get_plot(player)
				task.wait()
			until plot_folder ~= nil
			plot_folder = plot_folder.Objects
			
			MS.placeObjects(playerData.ShipSaves.AutoSave, plot_folder)
			
			print("Autosave data loaded")
			
		end
		sessionData[player.UserId] = playerData
	else
		warn("Unable to get data for", player.Name)
		player:Kick("Unable to load your data. Try again later.")
	end
	
	dollars.Value = sessionData[player.UserId].Dollars
	dollars:GetPropertyChangedSignal("Value"):Connect(function()
		sessionData[player.UserId].Dollars = dollars.Value
	end)
	
	rp.Value = sessionData[player.UserId].RP
	rp:GetPropertyChangedSignal("Value"):Connect(function()
		sessionData[player.UserId].RP = rp.Value
	end)
	
end
4 Likes

SetAsync Overwrites it!, use update async to compare data and then safely save it, + test it inside place, not studio! use prints to determine when script breaks

1 Like

I download the data, change it, then overwrite the data in the datastore. You would be correct, but this is not the issue. Thanks though!

2 Likes

If you’re running this in studio, you’re going to have to use BindToClose for it to save in studio as studio functions differently.

Alright will test this soon, thanks!

This fix did not work, and I do not know why…