How to avoid duplicating tools saved with ProfileService?

Well, I have a problem that I already put in a previous post, and it is the same. I figured out a way to prevent tools from duplicating, but I don’t see how secure it is, and that is that the data you previously had is deleted and then you take the tools from the backpack again and save them, but I just don’t like it because probably tools will be lost

This is what the ProfileService handles:

local Players = game:GetService("Players") 
local ProfileService = require(script.ProfileService)
local saveStructure = {
	Tools = {ToolsWithStacks = {}, ToolsWithoutStacks = {}};
	Money = 0;
	LogInTimes = 0;
	LogInGiift = 0;
	
	Shadows = "Medio";
	Water = "Medio";
	Tree = "Enabled";
	
	FPS = "UnEnabled";
	Ping = "UnEnabledPing";
	BrilloVol = 0.7;
	PlayerChose = "ForLocalPlayer";
	Language = "English";
	SlotsInventory = 90;
	
	Crouch = "Keep";
	Sprint = "Keep";
}

local PlayerProfileStore = ProfileService.GetProfileStore("test33", saveStructure)

local cachedProfiles = {}

local function PlayerAdded(player)
	local profile = PlayerProfileStore:LoadProfileAsync("Player_".. player.UserId, "ForceLoad")
	if profile ~= nil then
        profile:Reconcile()
		profile:ListenToRelease(function()
			cachedProfiles[player] = nil
			player:Kick("Tus datos no han sido cargados. Por favor únase nuevamente")
		end)
		if player:IsDescendantOf(Players) then
			cachedProfiles[player] = profile
			DoSomethingWithALoadedProfile(player, profile)
		else
			profile:Release()
		end
	else
		player:Kick("No se pueden cargar tus datos. Por favor únase nuevamente")
	end
end

for _, player in ipairs(Players:GetPlayers()) do
	coroutine.wrap(PlayerAdded)(player)
end

local function PlayerRemoving(player)
	local profile = cachedProfiles[player]
	
	profile.Data.Tools.ToolsWithStacks = {}
	profile.Data.Tools.ToolsWithoutStacks = {}
	for _,tool in pairs (player.Backpack:GetChildren()) do 
		if not tool:IsA("Tool") then continue end
		if tool:FindFirstChild("Stack") then
			table.insert(profile.Data.Tools.ToolsWithStacks, {ToolName = tool.Name, Stacks = tool.Stack.Value})
		else
			table.insert(profile.Data.Tools.ToolsWithoutStacks, {ToolName = tool.Name})
		end
	end
	if profile ~= nil then
		profile:Release()
	end
end

Players.PlayerAdded:Connect(PlayerAdded)
Players.PlayerRemoving:Connect(PlayerRemoving)

function cachedProfiles:Get(player, yield)
	local profile = cachedProfiles[player]
	if yield and not profile then
		repeat task.wait(0.1)
			profile = cachedProfiles[player]
		until profile or (not player.Parent)
	end
	if profile then
		return profile
	end
end

return cachedProfiles

But I only need to fix the following part of the above script:

local function PlayerRemoving(player)
	local profile = cachedProfiles[player]
	
	profile.Data.Tools.ToolsWithStacks = {}
	profile.Data.Tools.ToolsWithoutStacks = {}
	for _,tool in pairs (player.Backpack:GetChildren()) do 
		if not tool:IsA("Tool") then continue end
		if tool:FindFirstChild("Stack") then
			table.insert(profile.Data.Tools.ToolsWithStacks, {ToolName = tool.Name, Stacks = tool.Stack.Value})
		else
			table.insert(profile.Data.Tools.ToolsWithoutStacks, {ToolName = tool.Name})
		end
	end
	if profile ~= nil then
		profile:Release()
	end
end

As you can see, the tools are removed from the ProfileService and then the new ones are saved. It doesn’t seem to me, since I don’t want the data to be erased, I want it to make changes on save if there was any change with the tools (a tool was removed or added)

1 Like

You could perform a check for duplicate tools and then simply :Destroy() them or parent them to “Debris”.

I thought so, but I didn’t know how to do it