How To Check If Table Is Empty?

Hello everyone,

How to check if the table is empty?

Thanks!

8 Likes

If you know it is an array then the length of the table will be zero.

if (#Array == 0) then
    print("Array is empty")
end

If it is a dictionary you have to count the number of indices, or check if pairs’s iterator function returns anything.

local Dictionary = {}

--// EDIT: changed pairs(t)(t) to next(t) since pairs(t) basically just returns next...
if (next(Dictionary) == nil) then
    print("Dictionary is empty")
end
32 Likes

you can simply do

if #table == 0 then
    print("table is empty")
end
20 Likes

I wanna do a follow up on this if you can dm me:

ive got some code here:
image
it says this, and I’ve tried everything to try and detect if the table is empty, but it returns this no matter what. i know the table is empty but it throws an error if it is when I’m trying to address it and whatever I do before it to try and tell if it is it throws this error still

3 Likes

That means the table you’re trying to access is nil.

You should probably try something like this:

local table = {}

if table then
	if #table == 0 then
		print("table is empty")
	end
else
	warn("table is nil")
end
3 Likes

the table isnt nil because i printed it and its Table = {}, this isn’t a nil value this is an empty table.

1 Like

isnt table a reserved word? im a bit confused

1 Like

Replace table with your list.