Custom backpack skips a number

Char.ChildAdded:Connect(function(object)
	local isNew 
	local isOld 
	if object:IsA("Tool") then 
		for _,v in pairs(keyTable) do 
			if v["Tool"] == object then 
				isOld = true
			end
		end
		
		if not isOld then 
			if #toolCounter < 5 then 
				for i,v in pairs(keyTable) do 
					local tool = v["Tool"]
					if tool then 
						print(i)
						table.insert(numberCounter, tool)
						
					end
				end
				local number = #numberCounter
				print(number)
				keyTable[number + 1]["Tool"] = object
				
				for i,v in pairs(numberCounter) do 
					table.remove(numberCounter, i)
				end
								
				local textButton = Template:Clone()
				textButton.Parent = ScreenGui
				textButton.Visible = true
				textButton.TextLabel.Text = number + 1 
				
				local sizeX = textButton.AbsoluteSize.X
				local gapX = 10
				local totalX = sizeX + gapX
				
				
				textButton.Position = UDim2.new(0.25, (number)*totalX, 0.8, 0)
				
				textButton.MouseButton1Down:Connect(function()
					
				end)
			end
		end
	end
end)

Your numberCounter table seems to only be used for calculating the number of tools. It’s not necessary to have a table only for calculating the number of tools. It can also be done by creating a number variable and increasing its value when a tool is found.

I’m not sure what’s causing the problem, but the problem might be related to a loop problem. The pairs loop where you remove values from numberCounter might not remove every item. When a value is removed from an array with table.remove, the values that are stored in higher indexes get moved one index down. Because of that, when the loop goes to the next index, the value that was originally stored to the next index isn’t there anymore. the value is moved to the index where a value was removed. Because it moved to the latest index and the loop continues to the next index, the loop will skip that value. So after the loop ends, there will still be values in the table. This might not be a very good explanation, but I hope you can understand it. To remove things from an array without any of them being skipped, you can start from the last index, which is possible when you use a normal for loop.

for i = #array, 1, -1 do
	table.remove(array, i)
end

I’ll also ask a question. What is the toolCounter table? I don’t see it being modified anywhere and the line where it’s created isn’t posted either. Is it possible that it has something to do with the problem?

Anyways, I edited your code a bit so that it doesn’t insert the tools to a table to calculate them.

Char.ChildAdded:Connect(function(object)
	local isNew 
	local isOld 
	if object:IsA("Tool") then 
		for _,v in pairs(keyTable) do 
			if v["Tool"] == object then 
				isOld = true
				break
			end
		end
		
		if not isOld then 
			if #toolCounter < 5 then
				local number = 0
				for i,v in pairs(keyTable) do 
					local tool = v["Tool"]
					if tool then 
						print(i)
						number += 1
						
					end
				end
				print(number)
				keyTable[number + 1]["Tool"] = object
								
				local textButton = Template:Clone()
				textButton.Parent = ScreenGui
				textButton.Visible = true
				textButton.TextLabel.Text = number + 1 
				
				local sizeX = textButton.AbsoluteSize.X
				local gapX = 10
				local totalX = sizeX + gapX
				
				
				textButton.Position = UDim2.new(0.25, (number)*totalX, 0.8, 0)
				
				textButton.MouseButton1Down:Connect(function()
					
				end)
			end
		end
	end
end)
2 Likes