Why is my data not saving?

I’m currently making a datastore for my game, and I was recommended to use UpdateAsync() and also to save a table to a datastore instead of having like five of them. The issue is, when I try saving my table of data, it doesn’t seem to work…

There are no errors, but here is the script.

game.Players.PlayerAdded:Connect(function(player)
	local playerDataTable = {
		["studData"] = 0,

		["totalBricks"] = 0,

		["savedKillSound"] = shopModule.shopAssets.killSounds.default,

		["playerItemData"] = {
			Weapons = {},

			KillSounds = {},
		},

		["shopItemData"] = {},
	}
	
	local killSound = Instance.new("Sound", player)
	killSound.Name = "killSound"
	killSound.Volume = 2
	killSound.SoundId = game:GetService("ReplicatedStorage"):WaitForChild("KillSounds"):WaitForChild("default").SoundId

	local items = nil

	local leaderstats = Instance.new("Folder", player)
	leaderstats.Name = "leaderstats"

	local Studs = Instance.new("NumberValue", leaderstats) -- The Currency
	Studs.Name = "Studs"
	Studs.Value = 0

	local Bricks = Instance.new("IntValue", leaderstats) -- The Bricks broken between matches
	Bricks.Name = "Bricks"
	Bricks.Value = 0

	local totalBricks = Instance.new("IntValue", leaderstats) -- All time bricks broken
	totalBricks.Name = "T. Bricks"
	totalBricks.Value = 0
	
	local datatable
	local success, errorMessage = pcall(function()
		datatable = tableDataStore:GetAsync(player.UserId)
	end)

	if success then
		playerDataTable = datatable
	elseif errorMessage then
		warn(player.Name.."'s data cannot be found!..")
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local leaderstats = player.leaderstats
	
	local dataToSave = {
		["studData"] = player.leaderstats.Studs.Value,

		["totalBricks"] = player.leaderstats["T. Bricks"].Value,

		["savedKillSound"] = player.killSound.SoundId,

		["playerItemData"] = {
			Weapons = {},

			KillSounds = {},
		},

		["shopItemData"] = {},
	}
	
	print(dataToSave)
	
	
	tableDataStore:UpdateAsync(player.UserId, function(passData)
		return dataToSave
	end)

	local playerItemTable

	for i, v in pairs(shopModule.listOfPlayerItemTables) do
		if v.ownerTag == player.Name then
			playerItemTable = v
		end
	end
end)

DataStores cannot store Instances, try removing this and checking if it works.

But, thats a table created for loading, not the table when saving which is this:

	local dataToSave = {
		["studData"] = player.leaderstats.Studs.Value,

		["totalBricks"] = player.leaderstats["T. Bricks"].Value,

		["savedKillSound"] = player.killSound.SoundId,

		["playerItemData"] = {
			Weapons = {},

			KillSounds = {},
		},

		["shopItemData"] = {},
	}

Yeah I realized that right now. I feel dumb lol

Are you leaving the game, or are you closing the studio local server? Because you don’t have anything bound to game:BindToClose() so it won’t save on the studio server closing. Other than that I can’t see any issues.

I don’t see where you are even trying to use your loaded data. You set totalBricks.Value = 0 as an example before you even call :GetAsync and even after the data loads, you have no lines of code that use that data to override the default values.

Edit: You also need to add :BindToClose() like @sussyswif_t said above.

1 Like