Not being able to detect if there's content inside of a table

for Key,Slot in pairs(self.Data) do
				if #Slot == 0 then
					print(Slot,#Slot)
					if Slot == "Extra" then
						self.Data["Extra"][Item] = Amount
						break
					else
						CurrentSlot = Key
						self.NumSlots += 1
						self.Data[Key][Item] = Amount
						break
					end
				end
				Key += 1	
			end

This code is looping through slots for an inventory to find next empty one. if #Slot == 0 then is never false, and print(Slot,#Slot) prints {...}, 0 Never 1. Am I trying to detect the content in the wrong way, or does it have to do with the table?

What are the keys of your table? the # operator only works on contiguous lists. I.e. it will return 3 for a list-like table, {1 = a, 2 = b, 3 = c}, but only 2 for {1 = a, 2 = b, 3 = nil, 4 = d}, and zero for any table with only string keys.

It looks like your keys are strings, based on this line:

self.Data["Extra"][Item] = Amount

If you want to check if a dictionary is empty, use next().

if next(Slot) == nil then
-- Rest of code
end

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