i have this script that check if the player has a duplicated items in there backpack
if the script founds one it will delete the duplicated item
but it dosent work
this is the script
p = game.Players:GetChildren()
config = { tooltype1 = "Tool",
tooltype2 = "HopperBin",
waitt = 5,
}
tools = { }
while true do
wait(config.waitt)
for i = 1,#p do
if p[i].className == "Player" then
t = p[i].Backpack:GetChildren()
for i = 1, #t do
if (t[i].className == config.tooltype1) or (t[i].className == config.tooltype2) then
table.insert(tools, string.lower(t[i].Name))
end
end
table.sort(tools)
for i = 1, #tools do
if tools[i] == tools[i-1] then
t[i]:remove()
table.remove(tools, i)
end
end
end
tools = { }
end
end
-- reference to tool that you want to give to the player
local toolToGive = tool
-- try to find a tool with the same name as the tool that you want to give
local playerBackpackTool = player.Backpack:FindFirstChild(toolToGive.Name) or player.Character:FindFirstChild(toolToGive.Name)
-- if tool with same name doesn't exist then
if not playerBackpackTool then
-- give tool to player
toolToGive:Clone().Parent = player.Backpack
end
local Players = game:GetService("Players")
local Player = Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()
local Backpack = Player:WaitForChild("Backpack")
local function RemoveDuplicateTools()
local ToolQuantities = {}
local EquippedTool = Character:FindFirstChildOfClass("Tool")
if EquippedTool then
ToolQuantities[EquippedTool.Name] = 1
end
for _, Tool in ipairs(Backpack:GetChildren()) do
if not ToolQuantities[Tool.Name] then
ToolQuantities[Tool.Name] = 1
else
ToolQuantities[Tool.Name] += 1
end
end
for ToolName, ToolQuantity in pairs(ToolQuantities) do
if ToolQuantity > 1 then
for Index = 1, ToolQuantity - 1 do
local Tool = Backpack:FindFirstChild(ToolName)
if Tool then
Tool:Destroy()
end
end
end
end
end
RemoveDuplicateTools()
Character.ChildAdded:Connect(RemoveDuplicateTools)
Backpack.ChildAdded:Connect(RemoveDuplicateTools)
Tested and works, local script inside StarterCharacterScripts.