Delete duplicates from a folder

Hi, I am trying to make a simulator game, but the things that I bought from the store sometimes duplicates when it saves the things bought or because of another reason. I keep the tools that you bought in a folder and when it saves it transfers the tools to a table and it saves the table. This is the script I use for that:

local players = game:GetService("Players")
local dss = game:GetService("DataStoreService")
local ss = game:GetService("ServerStorage")
local location = game.ServerStorage.Tools
local location2 = game.ServerStorage.PlayerTools

local ds = dss:GetDataStore("Storage")
local data = {tools = {}}

game.Players.PlayerAdded:Connect(function(player)	
	player.CharacterAdded:Connect(function()

		spawn(function()
			while wait(90) do
				for i,v in pairs(location2:FindFirstChild(player.Name):GetChildren()) do
					if v:IsA("Tool") then
						table.insert(data.tools,v.Name)
					end
				end
				local suc, err = pcall(function()
					ds:SetAsync(player.UserId,data)
				end)

				if suc then
					table.clear(data.tools)
				elseif err then
					error("Error occured whilst saving data "..err)
				end
			end
		end)

		local tools = ds:GetAsync(player.UserId)
		if tools and tools.tools then
		for i,v in pairs(tools.tools) do
			wait()
			if location:FindFirstChild(v) then
				local clone = location:FindFirstChild(v):Clone()
				clone.Parent = game.ServerStorage.PlayerTools[player.Name]
				end
			end
		else
			local starterTool = game.ServerStorage.StarterTool:GetChildren()
			if starterTool:IsA("Tool") then
				starterTool:Clone()
				starterTool.Parent = player.Backpack
			end
			print("No tools stored")
		end
		
	end)
	end)

but somehow it duplicates the tools stored in that folder, thus making the table to big and thus adding the datastore request in a queue.

Is there any way to delete those duplicates with a script?

1 Like