How to clear rows of a table

I wanting to keep my players inventory clean and as ‘empty’ as possible, so when the player leaves I wanna basically shorten their inventory down.

Few key points

  1. Every 5 slots is a row/line
  2. Slots 1-30 should remain untouched

So quick example, let’s say this is the players Inventory when they leave.

 local Inventory = {
        [1] = {},
        [2] = {},
        [3] = {},
        [4] = {},
        [5] = {},
        [6] = {},
        [7] = {},
        [8] = {},
        [9] = {},
        [10] = {},
        [11] = {},
        [12] = {},
        [13] = {},
        [14] = {},
        [15] = {},
        [16] = {},
        [17] = {},
        [18] = {},
        [19] = {},
        [20] = {},
        [21] = {},
        [22] = {},
        [23] = {},
        [24] = {},
        [25] = {},
        [26] = {},
        [27] = {},
        [28] = {},
        [29] = {},
        [30] = {},
        [31] = {},
        [32] = {},
        [33] = {},
        [34] = {},
        [35] = {},
        [36] = {},
        [37] = {},
        [38] = {Id = "25", Quantity = 12},
        [39] = {},
        [40] = {},
        [41] = {},
        [42] = {},
        [43] = {},
        [44] = {},
        [45] = {}
   }

So slots 1-30, do not get touched, regardless if they empty or not.

Slots 31-35 (all of those slots are empty, so remove them) Slots 36-40 have an item (38) so we keep them, slots 41-45 are all empty, remove them.

However, when removing 31-35, 36-40 should move down and take their place. I don’t want

 local Inventory = {
        [1] = {},
        [2] = {},
        [3] = {},
        [4] = {},
        [5] = {},
        [6] = {},
        [7] = {},
        [8] = {},
        [9] = {},
        [10] = {},
        [11] = {},
        [12] = {},
        [13] = {},
        [14] = {},
        [15] = {},
        [16] = {},
        [17] = {},
        [18] = {},
        [19] = {},
        [20] = {},
        [21] = {},
        [22] = {},
        [23] = {},
        [24] = {},
        [25] = {},
        [26] = {},
        [27] = {},
        [28] = {},
        [29] = {},
        [30] = {},
        [36] = {},
        [37] = {},
        [38] = {Id = "25", Quantity = 12},
        [39] = {},
        [40] = {},
   }
1 Like

I think I finally got it, but I will apologize in advanced for the messy code. Not sure if this is the most efficient way of handling it, but it works!

Edit: deleted the inventory variable below since it was so big.
Edit 2: fixed a mistake in loop

-- call this function when you need to clean the inventory
function cleanInventory(inventory) -- input inventory into function
	local function isEmpty(item) -- a local function that checks to see if a slot it empty
		for _, value in pairs(item) do
			return false
		end
		return true
	end
	
	local size = #inventory
	local needToCheck = size - 30
	if size > 30 then -- made a check just in case?
		local check = 0
		local loop = true
		while loop do
			local canClear = true
			for i = 31 + check, 35 + check, 1 do
				if inventory[i] and not isEmpty(inventory[i]) then
					canClear = false
				elseif inventory[i] == nil then
					loop = false
				end
			end
			if canClear then
				for j = 1, 5 do
					table.remove(inventory, 31 + check)
				end
			end
			check += 5
		end
	end
	return inventory
end

cleanInventory(Inventory) -- input the table reference into the function
print(Inventory) 

Hope this helps! Let me know if something is wrong with the code.

1 Like

If you want to simulate a matrix and/or rows/columns you should be using arrays of arrays, or at least arrays with multiple values.

local Inventory = {
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{},
	{Id = "25", Quantity = 12},
	{},
	{},
	{},
	{},
	{},
	{},
	{}
}

local function clearTable()
	for i, v in ipairs(Inventory) do
		if i > 30 and (i - 1) % 5 == 0 then
			for i2 = 4, 0, -1 do
				if not Inventory[i + i2]["Id"] then
					if i2 == 0 then
						for i3 = 4, 0, -1 do
							table.remove(Inventory, i + i3)
						end
					end
				else
					break
				end
			end
		end
	end
end

clearTable()
print(Inventory[33]["Id"], Inventory[33]["Quantity"])
--25, 12 is output

I need them in an order, as when player rejoins it should remember the exact slot where each item is stored

The way you indexed your array (with indices using integer values 1 through n, with n being the length of the array) is the default way arrays are indexed.

local Inventory = {
	[1] = {},
	[2] = {},
	[3] = {},
	[4] = {},
	[5] = {},
	[6] = {},
	[7] = {},
	[8] = {},
	[9] = {},
	[10] = {},
	[11] = {},
	[12] = {},
	[13] = {},
	[14] = {},
	[15] = {},
	[16] = {},
	[17] = {},
	[18] = {},
	[19] = {},
	[20] = {},
	[21] = {},
	[22] = {},
	[23] = {},
	[24] = {},
	[25] = {},
	[26] = {},
	[27] = {},
	[28] = {},
	[29] = {},
	[30] = {},
	[31] = {},
	[32] = {},
	[33] = {},
	[34] = {},
	[35] = {},
	[36] = {},
	[37] = {},
	[38] = {Id = "25", Quantity = 12},
	[39] = {},
	[40] = {},
	[41] = {},
	[42] = {},
	[43] = {},
	[44] = {},
	[45] = {}
}

local function clearTable()
	for i, v in ipairs(Inventory) do
		if i > 30 and (i - 1) % 5 == 0 then
			for i2 = 4, 0, -1 do
				if not Inventory[i + i2]["Id"] then
					if i2 == 0 then
						for i3 = 4, 0, -1 do
							table.remove(Inventory, i + i3)
						end
					end
				else
					break
				end
			end
		end
	end
end

clearTable()
print(Inventory[33]["Id"], Inventory[33]["Quantity"])
--25, 12 is output

This produces the same result.