Item is duplicating itself in my inventory when I swap positions

I have an inventory system where players can move an item to a slot if it is empty, however, it seems like my items are duplicating when I move them even though I’m removing them from the table before re-adding them with a new index.

function Inventory:RemoveItem(item)
	for i, v in pairs(self.items) do
		if v == item then
			table.remove(self.items, i)
		end
	end
end

function Inventory:MoveItem(item, index)
	if index < 1 then return false end
	if index > self.slots then return false end
	if self.items[index] then return false end
	for i, v in pairs(self.items) do
		if v == item then
			self:RemoveItem(item)
			table.insert(self.items, index, item)
			return true
		end
	end
	return false
end

is there <instance>:Clone()?

Technically the instance isn’t cloning, but when I move the item to a separate slot, it seems to add it twice.

I’m not doing anything with the instance, just adding it to a table called self.items, and giving it a specific index relative to its’ slot number.

Replaced table.remove() with self.items[I] = nil
Apparently table.remove does this thing where it “shifts later elements down to fill in the empty space if possible.”

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.