Why is table only returning 1?

--[[
This is what 'PlayersInventory' looks like:
    Main = {
		[1] = {Type = 'Weapon', Name = 'Wooden Sword', Durability = 50},
		[2] = nil,
		[3] = {Type = 'Material', Name = 'Wood', Quantity = 4},
		[4] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 50},
		[5] = nil			
	},
	Stored = {
		[1] = nil,
		[2] = nil,
		[3] = nil,
		[4] = nil,
		[5] = nil,
		[6] = nil,
		[7] = nil,
		[8] = nil,
		[9] = nil,
		[10] = nil,			
	}
]]

local PlayersInventory = InventoryCheck:InvokeServer()

local function Setup()
	-- Setup main slots
	print(#PlayersInventory.Main)
	for i, v in pairs(PlayersInventory.Main) do
		print(i, v)
	end
end

Setup()

When I go print(#PlayersInventory.Main) it should print 5, and go through all slots of the Main table. However it just prints 1. Iā€™m guessing it has something to do with the nil settings? I do that because if that slot is left empty then it needs to stay empty.

I agree, the index at 2 set to nil is most likely the cause. The operation # uses iteration and will terminate on the first nil index. So you should either specify a constant table size or use pairs.

4 Likes

Figured to just use an empty table

[1] = {Type = 'Weapon', Name = 'Wooden Sword', Durability = 50},
[2] = {},
[3] = {Type = 'Material', Name = 'Wood', Quantity = 4},
[4] = {Type = 'Tool', Name = 'Wooden Axe', Durability = 50},
[5] = {}

Seemed to work

2 Likes