UpdateAsync() Error?

Hello fellow developers! Datastore is one of the most important services in a Roblox game. I’m trying to use UpdateAsync() instead of SetAsync().

This is a function which saves player’s data when they leave the game.

local function saveData(player,tableToSave)
	print("Function Executed")
	local success, err = pcall(function()
		dataStore:UpdateAsync(player.UserId, function(compareTable)
			print(compareTable)
			if compareTable == nil then --NewPlayer
				local NewTableToSave = tableToSave
				
				print(NewTableToSave)
				return NewTableToSave
				
			elseif compareTable then
				print(compareTable)
				print(tableToSave)
				return tableToSave
			end
		end)
	end)
	
	print("Code Executed")
	if success then 
		print(player,"'s Data has been saved!")
	else 
		print(player,"'s Data hasn't been saved!")
		warn(err)		
	end
end

This is the line which calls the function:

game.Players.PlayerRemoving:Connect(function(player)
          print(player)
          local success, err  = pcall(function()
		    print(player,"SuddenContinued")
		    saveData(player,{
			["Leaderstats"] = {["UCoins"] = LeaderstatsFol.UCoins.Value, ["Diamonds"] = LeaderstatsFol.Diamonds.Value},
			["PlayerOverallStats"] = {
				["Tutorial"] = SaveDataFol:FindFirstChild("TutorialDone").Value
			},
			["Inventory"] = {
				["Hotbar"] = {
					[1] = {["WeaponName"] = HotbarFol:FindFirstChild("Hotbar1"):FindFirstChild("WeaponName").Value, ["Type"] = HotbarFol:FindFirstChild("Hotbar1"):FindFirstChild("Type").Value},
					[2] = {["WeaponName"] = HotbarFol:FindFirstChild("Hotbar2"):FindFirstChild("WeaponName").Value, ["Type"] = HotbarFol:FindFirstChild("Hotbar2"):FindFirstChild("Type").Value},
					
				},
				["InventoryBackpack"] = {
					["Melee"] = GetInvWeapon("MeleeWeapon"),
					
					
				},
			},

		}) -- Save the data
	end)
	
	print(player,"Continued2")
	
	if success then -- If the data has been saved
		print(player,"'s Data has been saved!")
	else -- Else if the save failed
		print(player,"'s Data hasn't been saved!")
		warn(err)		
	end
	
	print(player,"Continued3")
end)

It prints player, the “SuddenContinued” and “Function Executed”)
But it doesn’t print compareTable, “Continue3” and the other else.
Anyone know why?
Did I do anything wrong while coding this?
Any comments are appreciated!

(Edit: I just make the layout better so it is easier for you to read.)

You should get rid of the pcall(function() before you use your saveData function as it really doesn’t do anything since you’re already using a pcall inside of the function in case UpdateAsync errors.
Also what is compareTable exactly, I don’t think its inside of the code that you showed us. I might have an idea why it won’t work but I’m not sure since I don’t know what compareTable is exactly.

I tried to remove all the pcall functions in the script but it doesn’t have any errors. It is weird because sometimes everything works fine, everything was printed and the data were saved. Sometimes it doesn’t print until “Function Executed” so the data weren’t saved.
Also compareTable is the function’s name/parameter? I have never defined compareTable anywhere.

I tried the script and everything works when I changed the table to save so I think the problem is in the table

Could you test the script multiple times? Because it is like by chances. Sometimes it works, sometimes it doesn’t work.