UpdateAsync Not Working

I can not figure out why my code is not working:

local DefaultData = {
	
	["CurrentStats"] = {
		
		["CityLevel"] = 0,
		["CityPopulation"] = 0,
		["CityVisits"] = 0,
		["CoinsPerSecond"] = 0,
		["Coins"] = 0,
		["Cash"] = 0,
		["AchievementsCompleted"] = 0,
		["HasAirport"] = false,
		["HasForeverDoubleEarnings"] = false,
		
	},
	
	["HighestStats"] = {
	
		["HighestCityLevel"] = 0,
		["HighestCityPopulation"] = 0,
		["HighestCoins"] = 0,
		["HighestCash"] = 0,
		["HighestAchievementsCompleted"] = 0	
		
	}
	
	
}

local PlayerDataTable

game:GetService("Players").PlayerAdded:Connect(function(Player)
	
	print(Player.Name)
	
	local playerKey = tostring("key-"..Player.UserId.."-devtechgames")
	local playerData
	
	local success,err = pcall(function()
		
		local playerData = PlayerStats:GetAsync(playerKey) or DefaultData
		
	end)
	
	if success then
		if playerData then
			PlayerDataTable = playerData
			print("Player has data")
		else
			print("Player has no data")
			print(playerData)
		end
		
	end
	
end)

game:GetService("Players").PlayerRemoving:Connect(function(Player)
	
	local playerKey = tostring("key-"..Player.UserId.."-devtechgames")
	
	local success,err = pcall(function()
		PlayerStats:UpdateAsync(playerKey,function(oldData)
			
			local newData = oldData or DefaultData
			print("Saved data")
			return newData
			
		end)
	end)
	
end)

This is the output:
image
Every time i join it is saying the data is non existent even though it should be there.

EDIT:
I do have API services enabled

You are using update, even when i have no data saved, so when i am new, you must save default data to me (into datastore) (this is easiest, because it is just one line)
or you can solve this as i do (not recomended) by using setAsync

How do i save data without using Update or Set; I think I am misunderstanding what you are trying to say, however, I understand now that it doesnt work because there is no data.
How can this be done?

Look, you cant use update on non existing slot in database, so you need to save there (using set) at least the default data, when completly new player join

so you need to add here PlayerStats:SetAsync(playerKey,DefaultData)
sry for mistakes, i am on mobile

1 Like