Unable to get Item if it a nil value

I can’t get Item if it nil, how to fix it?

local ItemList = {Pet1 = "Mouse", Pet2 = nil, Pet3 = nil}

for i, petName in next, ItemList do
print(petName)
end

-- It only print the Pet1, i want it print Pet2 and Pet3 too.

if a value in your table is nil in reality the index exists (kind of) yet the value doesn’t “exist” or will not exist (if you choose to use nil to remove values) because nil is essentially “nothing” or “undefined” , making Pet2 for example “nothing” if that makes sense. But anyways i’m not sure what this is for but instead try “setting your indexes” to an actual “value” like false or use a string to represent undefined values like: {Pet3 = "Undefined") but if you use that you will have to check if value == "Undefined"

This explains a little more about using nil in tables and how it’s used to remove values:
http://www.lua.org/pil/2.5.html

Ok thanks i will try it. [30…]