Wipe only one part of data from DataStore? (ProfileService)

So we need to wipe only a specific part of player’s data, which would be the “pets” section.
image

Is there any way to make ONLY that part be wiped and the rest stay as it was?

Just ignore that data and don’t save any part of it, it’ll work itself out :slight_smile:

When you say wipe, are you referring to wiping the data assigned to the variable or are you referring to setting the variable to a value of nil?

I’m assuming you just mean restoring the tables back to their ProfileTemplate data.
If so heres how I would do it:

local function DeepCopyTable(table)
	local copy = {}
	for key, value in pairs(table) do
		if type(value) == "table" then
			copy[key] = DeepCopyTable(value)
		else
			copy[key] = value
	end
	return copy
end

local function WipeData(player: Player)
	local profile = Profiles[player]
	profile.Data.Pets = DeepCopyTable(ProfileTemplate.Pets)
	profile.Data.PetsEquipped = DeepCopyTable(ProfileTemplate.PetsEquipped)
	profile.Data.PetsUnlocked = DeepCopyTable(ProfileTemplate.PetsUnlocked)
	profile.Data.PetsCapacity = 50
	profile.Data.PetsEquipLimit = 4
end

The DeepCopyTable function serves to make an exact duplicate of a Lua table. This means it not only clones the top-level table but also creates new instances of nested tables and their content. It ensures that any modifications made to the copy won’t affect the original data structure.

In the WipeData function, the goal is to reset or initialize a player’s data, guided by a predefined ProfileTemplate. When provided with a player object as an argument, the function locates the player’s profile and takes the following actions:

  • The profile.Data.Pets table is reset to a deep copy of ProfileTemplate.Pets. This safeguards against any changes made to profile.Data.Pets influencing the original ProfileTemplate.Pets.
  • Similarly, profile.Data.PetsEquipped is set to a deep copy of ProfileTemplate.PetsEquipped, and profile.Data.PetsUnlocked to a deep copy of ProfileTemplate.PetsUnlocked.
  • Additional player-specific properties like PetsCapacity and PetsEquipLimit are also initialized or reset to specific values.

By utilizing these deep copies of the ProfileTemplate data, the code ensures that each player’s data starts fresh or is consistently restored to a known state, which can be essential for maintaining game balance and data integrity.

Sorry for a late response, I found the solution myself. Will post it for others to see later.

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