How would I go about only saving keys that aren't empty in a table to DSS?

DSS = DataStoreService

Hello! So I have a function that saves {["firstTimeJoined"]: number, ["totalXP"]: number, ["points"]: number} to a DSS, except that I want to only save [“points”] sometimes, how would I do that?

Like calling DataStoreManager.savePlayerData(player, {["points"] = 78}) with only the points value, but it not overriding the others?

Here’s my code:

function DataStoreManager.savePlayerData(player: Player, playerData: {["firstTimeJoined"]: number, ["totalXP"]: number, ["points"]: number}): boolean
	local maxTries: number = 5
	local success: boolean
	
	
	
	repeat
		maxTries -= 1
		success = pcall(function()PlayersStatsStore:SetAsync(player.UserId, playerData)end)
	until success or maxTries == 0

	return success
end

Thank you for helping!

I used UpdateAsync() to fix the issue. Completely forgot about it!

function DataStoreManager.savePlayerData(player: Player, playerData: {["firstTimeJoined"]: number?, ["totalXP"]: number?, ["points"]: number?}): boolean
	local maxTries: number = 5
	local success: boolean

	repeat
		maxTries -= 1
		success = pcall(function()
			PlayersStatsStore:UpdateAsync(player.UserId, function(currentPlayerData)
				for count, key in pairs(playerData) do
					currentPlayerData[count] = key
				end
				return currentPlayerData
			end)
		end)
	until success or maxTries == 0

	return success
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.