How could I find the amount of unique items there are in a Player's Backpack?

How could I find how many unique items there are in a Player’s Backpack? I have tried searching for solutions but I couldn’t find any… I assume you could first get Backpack’s children + the tool that the player is holding, and then sift through the list and remove duplicates, but I don’t know how to do this.

What do you mean by unique items?

To get the total amount of tools in the player’s backpack :

#Player.Backpack:GetChildren()

From reading the rest, I found out that he probably means how to filter out the duplicate items.

1 Like

The amount of separate items. For example, if you were to have a minecraft style inventory system, I want to find the amount of slots that the inventory takes up.

Here’s a solution that might work (LocalScript):

local player = game.Players.LocalPlayer
local gearlist = {}
for _, gear in pairs(player.Backpack:GetChildren()) do
	if not table.find(gearlist, gear) > 0 then
		table.insert(gearlist, gear)
	else
		gear:Destroy()
	end
end

This should filter out gears that are duplicates.

There is an issue with this, it’s that all instances are unique. If you have 2 duplicated sword items it means if you check is there sword 2 in the table it will not find it since there is only sword 1. You should use the tool’s names instead.

That might lead to accidental deletion. If a gear has the same name but not the same contents, it might be mistaken for another.

But, in that case, you can do this edit:

local player = game.Players.LocalPlayer
local gearlist = {}
for _, gear in pairs(player.Backpack:GetChildren()) do
	if not table.find(gearlist, gear.Name) > 0 then
		table.insert(gearlist, gear.Name)
	else
		gear:Destroy()
	end
end
local function GetDuplicateTools(Player)
	local DuplicateTools = {}
	local ToolNames = {}
	
	local Character = Player.Character
	local Backpack = Player:FindFirstChildOfClass("Backpack")
	if not (Character and Backpack) then return end
	local Tools = Backpack:GetChildren()
	local Tool = Character:FindFirstChildOfClass("Tool")
	if Tool then table.insert(Tools, Tool) end
	
	for _, Tool in ipairs(Tools) do
		if ToolNames[Tool.Name] then table.insert(DuplicateTools, Tool) end
		ToolNames[Tool.Name] = true
	end
	
	return DuplicateTools
end

I took this as you wanted to know how many items you considered to be unique were in the players possession. Not duplicates … Doh!

Here’s an hours worth of research :unamused:


local player = game.Players.LocalPlayer
local gearlist = {}
local uniques = {
	"Sleep Potion",
	"Brown Bread",
	"ClassicSword"
}

local tool = player.Character:FindFirstChildOfClass("Tool")
if tool ~= nil then
	table.insert(gearlist, tool)
end

local function findValue(whichArray, itemName)
	for currentIndex = 1, #whichArray do
		if whichArray[currentIndex] == itemName then
			return currentIndex
		end
	end
end

for _, gear in pairs(player.Backpack:GetChildren()) do
	if not table.find(gearlist, gear.Name) == true then
		if findValue(uniques, gear.Name) ~= nil then
			table.insert(gearlist, gear.Name)
		end -- else gear:Destroy()
	end
end

print(gearlist)