Table length is 0 but it has data? (Confusion)

Hello! I am trying to make a system that if you get a pet it unlocks in the gui . APARENTLY it works because the cat and dog have true but the unicorn have false.

image

This table is what I get from datastore, the length is 0 but it has information, also that For prints nothing, so this situation is confusing me a lot

    print("table lenght: "..#tablePetHide)
	print(tablePetHide["Cat"])
	print(tablePetHide["Dog"])
	print(tablePetHide["Unicorn"])
	
	for i, v in ipairs(tablePetHide) do
		print(v[1]) --nothing happens here
	end

prints:

table length: 0
true
true
false

Table length only works with integer indices.

t = {true, true, false}

If you are using string keys (Cat, Dog, Unicorn) you will need to get the length with a for loop.

1 Like

Because it doesn’t work the same way with keys and values (dictionaries)

1 Like

well the simplest way to do it was to see if they have at least 1 pet.
There wouldnt be a real need to get the entire length. Just check if it has 1 value.

if TablePetHide[1] == nil then
    --- You dont have pets
else
     --- You have pets
end

That won’t work since he is using string keys. That’s the whole reason he can’t get the length.
He can use next(TablePetHide)==nil though.

5 Likes

this would work, no matter the value put in.
its checking if it has any kind of value in TablePetHide[1]. [1] is the first assigned slot.
if theres a string or a number or another array, then [1] wont be nil.
however if there is no values in the array then [1] would be nil

No, it definitely will not work. You can’t access a dictionary with an integer key if there are no entries stored with integer keys. Go check for yourself if you don’t believe me. Here are all of the outputs in the original post, as well as your suggestion and mine.

2 Likes

well its not nil is it. if theres no entrys it will be nil. meaning it works for the purpose.

Slow down and read the code. There are three entries. tablePetHide[1] is nil despite that. I’m done discussing this.

4 Likes