How to avoid duplicating items when Saved with ProfileService?

Well sorry for those who tried to help me, I just can’t think of a way to avoid duplicating saved Tools. My problem is that the tools are duplicated when entering the game (if you have saved tools they will be duplicated), this happens because the part of applying the tools to the backpack and updating the tools is executed at the same time (when loading the tools, the part that detects the tools that were added, saves them, and then starts a tool duplication cycle). I don’t have the ability to think of a solution to this.

local function GetTools(player, profile)
	if profile ~= nil then
		for _, Tools in pairs(profile) do
			if not toolsFolder:FindFirstChild(Tools) then continue end
			local Tool = toolsFolder[Tools]:Clone()
			Tool.Parent = player:WaitForChild("Backpack")
			local ToolGear = toolsFolder[Tools]:Clone()
			ToolGear.Parent = player:WaitForChild("StarterGear")
		end
	end
end

Players.PlayerAdded:Connect(function(player)
	local Profile = DataManager:Get(player, true)
	if not Profile then
		repeat task.wait(0.1)
			Profile = DataManager:Get(player, true)
		until Profile or (not player.Parent)
	end
	if Profile then
		GetTools(player, Profile.Data.Tools)
		player:WaitForChild("Backpack").ChildAdded:Connect(function()
			for _, Tool in pairs(player:WaitForChild("Backpack"):GetChildren()) do
				if not Tool:IsA("Tool") then continue end
				table.insert(Profile.Data.Tools, Tool.Name)
			end
		end)
	end
1 Like

Well it’s pretty obvious because everytime you respawn the tools get cloned from StarterGear to Backpack and you are constantly saving them.

I know, but I can’t find a way for that not to happen

Just save the tools when player left and check if player does not own the tool already.

game:GetService("Players").PlayerRemoving:Connect(function(player)
      local Profile = -- get your profile
      for _,tool in next,player.Backpack:GetChildren() do 
             if (table.find(Profile.Data.Tools,tool.Name)) then
                   continue -- skip the certain tool because player already owns it
             end
             -- your save method 
      end
end)

Well it gives me an error in the next part, I guess I should change it to pairs

I did so, although it does not save and it gives me an error

local function PlayerRemoving(player)
	local profile = cachedProfiles[player]
	for _,tool in pairs (player.Backpack:GetChildren()) do   
		if (table.find(profile.Data.Tools,tool.Name)) then
			continue
		end
		if not tool:IsA("Tool") then end-- skip the certain tool because player already owns it
		table.insert(profile.Data.Tools, tool)
	end
	if profile ~= nil then
		profile:Release()
	end
end

warn: [ProfileService]: DataStore API error [Store:"test26";Key:"Player_681953005"] - "104: Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters."
Error: Not running script because past shutdown deadline (x27)
These errors are given to me when saving

Oh yeah you need to save tool.Name not tool itself.

Oh it’s true. I will change that

You need to change it to

if not tool:IsA("Tool") then continue end

I don’t know if using PlayerRemovin with ProfileService when saving tools is a good option, but it works for now, so I’ll use it., but for now it works, so I will use it.

In fact I had it like that, but I saw that there was already a continue and well I thought it was not necessary