Troubles checking against a dictionary

Hi everyone! ^-^

I’ve been trying to create a custom backpack system, and whenever a tool comes into the players inventory, it gets added to the list. However, it keeps duplicating because it’s reading the same tools over again.

THE QUESTION: What would I do to have it check if the tool already exists in the dictionary??

local lplayer = game:GetService("Players").LocalPlayer
local backpack = lplayer.Backpack

local gui = script.Parent
local toollist = gui.ToolList
local template = gui.ToolList.template

local toolsavaliable = 0

local tools = {
	
	--tools get put into here when detected

}

--print(backpack:GetChildren())

backpack.ChildAdded:Connect(function(instance)
	
	for i, v in pairs(backpack:GetChildren()) do
		
		if instance then
			
			print(instance.Name .. " was added to the players inventory!")
			
			toolsavaliable = toolsavaliable + 1
			
			table.insert(tools, instance) --adds tool into the dictionary, whilst getting the total # of tools avaliable to the player
			
			print(tools)
			
			local templatecopy = template:Clone()
			templatecopy.Parent = gui.ToolList
			templatecopy.Name = instance.Name
			templatecopy.Text = instance.Name .. " - " .. toolsavaliable
			templatecopy.Visible = true
			
		end
		
	end
	
end)
2 Likes
if table.find(tools, instance) then else
2 Likes

You’re a god, thank you so much :sweat_smile:

1 Like