How to make sure a tool is not being inserted into a table multiple times

Hello so I ran into this problem where when the script checks the player’s backpack, the tools detected are inserted more than once, for example I have a mop in my backpack, the script inserts the mop to a table multiple times, this is because the check backpack script is in a loop to make sure all tools are detected just incase a tool is added later on the game. How do I make it so the tool is only inserted once.


local function checkBackpack()
	 
	for i, v in pairs(backpack:GetChildren()) do
			if v.ClassName == "Tool" then
				table.insert(InventoryItems, v)
			end
	end
	
	for i, v in pairs(Character:GetChildren()) do
		if v.ClassName == "Tool" then
			table.insert(InventoryItems, v)
		end
	end
	
end
while wait(0.1) do
     checkBackpack()

Hey there,

I would create a function to check if it’s data is in the table, and if it finds the data then it won’t add it in. If it helps you can also do a task.wait(n) in it to.

Feel free to let me know if you have any problems.

1 Like

How do I make a function to check if the data was already added

Just put this line before the line where you add them to the table.

if table.find(InventoryItems , tool) then return end
1 Like

Hey there,

I would create a function that is called checkIfInData() and then code it in to check every heart beat of so if the data is in the table to not re add it.

1 Like

Simply just check if the tool is already in the table. If yes then just stop the thing from adding.
For this purpose, we use table.find(table, thingtofind) which returns a number position. It will be nil if the thing to find isn’t in the table but if it is in the table, the value won’t be nil meaning you can stop the code from running further if you use return if the script has a value from the table.find statement which is not nil.

local function checkBackpack()
	 
	for i, v in pairs(backpack:GetChildren()) do
			if v.ClassName == "Tool" then
                if table.find(InventoryItems, v) then return end -- stops if the tool is found in the table
				table.insert(InventoryItems, v)
			end
	end
	
	for i, v in pairs(Character:GetChildren()) do
		if v.ClassName == "Tool" then
            if table.find(InventoryItems, v) then return end -- stops if the tool is found in the table
			table.insert(InventoryItems, v)
		end
	end
	
end
1 Like

Like this

local function checkBackpack()
	 
	for i, v in pairs(backpack:GetChildren()) do
		if v.ClassName == "Tool" then
          	  if table.find(InventoryItems, v) then return end
				table.insert(InventoryItems, v)
			end
	end
	
	for i, v in pairs(Character:GetChildren()) do
		if v.ClassName == "Tool" then
		if table.find(InventoryItems, v) then return end
			table.insert(InventoryItems, v)
		end
	end
	
end
1 Like

This is what happens when I added the line:


And if it’s not that then it’s blank/empty, Let me show you guys the full script.


local slots = (hotBar)
local hotbarItems = {}


local InventoryItems = {}

local backpack = Player.Backpack

local function checkBackpack()

	for i, v in pairs(backpack:GetChildren()) do
		if v.ClassName == "Tool" then
			if table.find(InventoryItems, v) then return end
			table.insert(InventoryItems, v)
		end
	end

	for i, v in pairs(Character:GetChildren()) do
		if v.ClassName == "Tool" then
			if table.find(InventoryItems, v) then return end
			table.insert(InventoryItems, v)
		end
	end

end

local itemChosen = nil
local human = Character:WaitForChild("Humanoid")

task.spawn(function()
	
	while task.wait(0.1) do
		
		checkBackpack()
		
		local limit = #InventoryItems
		if limit == 0 then continue end
		
		
		for i, v in pairs(InventoryItems) do
			
			local newSample = sample:Clone()
			
			newSample.Parent = script.Parent.BackpackUI.ScrollingChoices
			newSample.Name = v.Name; newSample.Visible = true
			newSample.Image = v.TextureId
			
			local backpackRemove = table.remove(InventoryItems, 1)
			
			if not backpackRemove then continue end
			
			itemChosen = newSample.MouseButton1Click:Connect(function()
				script.Parent.Backpack:TweenPosition(UDim2.new(0.394, 0,0.824, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
				if #hotbarItems == 0 then
					wait(0.2)
					hotBar.Visible = true
					hotBar.Image = newSample.Image
					
					hotBar.Item.Value = backpack[newSample.Name]
					table.insert(hotbarItems, newSample)
					newSample.Parent = script.HoldingOn
					local tool = hotBar.Item.Value
					human:EquipTool(backpack[newSample.Name])
					
					elseif #hotbarItems == 1 then
					human:UnequipTools()
					for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
						leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
					end
					
					hotBar.Image = newSample.Image
					hotBar.Item.Value = backpack[newSample.Name]
					human:EquipTool(backpack[newSample.Name])
						newSample.Parent = script.HoldingOn
						
				end
			end)
			
			
			script.Parent.Hotbar.Exit.MouseButton1Click:Connect(function()
				script.Parent.Backpack:TweenPosition(UDim2.new(0.467, 0,0.826, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
				for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
					human:UnequipTools()
					leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
					table.remove(hotbarItems, 1)
					
					hotBar.Image = "rbxassetid://10453314827"
					
					hotBar.Item.Value = nil
						hotBar.Visible = false
				end
			end)
		end
	end
	
end)

Use this

local slots = (hotBar)
local hotbarItems = {}


local InventoryItems = {}

local backpack = Player.Backpack

local function checkBackpack()

	for i, v in pairs(backpack:GetChildren()) do
		if v.ClassName == "Tool" then
			if table.find(InventoryItems, v) then continue end
			table.insert(InventoryItems, v)
		end
	end

	for i, v in pairs(Character:GetChildren()) do
		if v.ClassName == "Tool" then
			if table.find(InventoryItems, v) then continue end
			table.insert(InventoryItems, v)
		end
	end

end

local itemChosen = nil
local human = Character:WaitForChild("Humanoid")

task.spawn(function()
	
	while task.wait(0.1) do
		
		checkBackpack()
		
		local limit = #InventoryItems
		if limit == 0 then continue end
		
		
		for i, v in pairs(InventoryItems) do
			
			local newSample = sample:Clone()
			
			newSample.Parent = script.Parent.BackpackUI.ScrollingChoices
			newSample.Name = v.Name; newSample.Visible = true
			newSample.Image = v.TextureId
			
			local backpackRemove = table.remove(InventoryItems, 1)
			
			if not backpackRemove then continue end
			
			itemChosen = newSample.MouseButton1Click:Connect(function()
				script.Parent.Backpack:TweenPosition(UDim2.new(0.394, 0,0.824, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
				if #hotbarItems == 0 then
					wait(0.2)
					hotBar.Visible = true
					hotBar.Image = newSample.Image
					
					hotBar.Item.Value = backpack[newSample.Name]
					table.insert(hotbarItems, newSample)
					newSample.Parent = script.HoldingOn
					local tool = hotBar.Item.Value
					human:EquipTool(backpack[newSample.Name])
					
					elseif #hotbarItems == 1 then
					human:UnequipTools()
					for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
						leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
					end
					
					hotBar.Image = newSample.Image
					hotBar.Item.Value = backpack[newSample.Name]
					human:EquipTool(backpack[newSample.Name])
						newSample.Parent = script.HoldingOn
						
				end
			end)
			
			
			script.Parent.Hotbar.Exit.MouseButton1Click:Connect(function()
				script.Parent.Backpack:TweenPosition(UDim2.new(0.467, 0,0.826, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
				for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
					human:UnequipTools()
					leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
					table.remove(hotbarItems, 1)
					
					hotBar.Image = "rbxassetid://10453314827"
					
					hotBar.Item.Value = nil
						hotBar.Visible = false
				end
			end)
		end
	end
	
end)

It’s still happening when I tried to use continue end

What exactly is the problem here?

I’ve printed #InventoryItems and I get one so I’m now thinking it’s something to do with, can you take a look:

task.spawn(function()
	
	while task.wait(0.1) do
		
		checkBackpack()
		
		local limit = #InventoryItems
		if limit == 0 then continue end
		
		
		for i, v in pairs(InventoryItems) do
			
			local newSample = sample:Clone()
			
			newSample.Parent = script.Parent.BackpackUI.ScrollingChoices
			newSample.Name = v.Name; newSample.Visible = true
			newSample.Image = v.TextureId
			
			local backpackRemove = table.remove(InventoryItems, 1)
			
			if not backpackRemove then continue end
			
			itemChosen = newSample.MouseButton1Click:Connect(function()
				script.Parent.Backpack:TweenPosition(UDim2.new(0.394, 0,0.824, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
				if #hotbarItems == 0 then
					wait(0.2)
					hotBar.Visible = true
					hotBar.Image = newSample.Image
					
					hotBar.Item.Value = backpack[newSample.Name]
					table.insert(hotbarItems, newSample)
					newSample.Parent = script.HoldingOn
					local tool = hotBar.Item.Value
					human:EquipTool(backpack[newSample.Name])
					
					elseif #hotbarItems == 1 then
					human:UnequipTools()
					for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
						leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
					end
					
					hotBar.Image = newSample.Image
					hotBar.Item.Value = backpack[newSample.Name]
					human:EquipTool(backpack[newSample.Name])
						newSample.Parent = script.HoldingOn
						
				end
			end)
			
			
			script.Parent.Hotbar.Exit.MouseButton1Click:Connect(function()
				script.Parent.Backpack:TweenPosition(UDim2.new(0.467, 0,0.826, 0),Enum.EasingDirection.In,Enum.EasingStyle.Sine, 0.2,true)
				for i, leftinStorage in pairs(script.HoldingOn:GetChildren()) do
					human:UnequipTools()
					leftinStorage.Parent = script.Parent.BackpackUI.ScrollingChoices
					table.remove(hotbarItems, 1)
					
					hotBar.Image = "rbxassetid://10453314827"
					
					hotBar.Item.Value = nil
						hotBar.Visible = false
				end
			end)
		end
	end
	
end)

This is the problem:
image

What is it, that the items are outside of the ui?

1 Like

I’m not sure, not really concerned about that yet, but I am concerned about the multiple times the button is being cloned

1 Like

Its because you are cloning the button in your loop, you should make it clone when a child is added and delete when a child is removed.

The table is fine, its that you made it clone every time you loop

How would I make it clone when a child is added and delete when child is removed

Use

Backpack.ChildAdded:connect(function(child)

end

Backpack.ChildRemoved:connect(function(child)

end

And inside check if its in the table, if it is then dont clone if it isn’t in the table than add it and clone, in the removing just remove its button and remove it from the table.

1 Like