How do I check if a table is empty?

So I have a script that creates a HighLight when a player hovers over specific objects with their mouse, I then store that HighLight in a table to destroy() is later when the player is no longer hovering over the object, the problem is the script always runs using RenderStepped, I need to check if the table is empty before the script can continue to run.

local Table = {}

if #Table == 0 then 
	print("Empty Table")
end
1 Like

If your table is an Array, you can simply put the hashtag character before its name eg.

local array = {}
table.insert(array, "any")

print(#array) -- outputs 1.

if you are trying to count indexes inside a dictionary/mixed table, you can use a for loop like:

local table = {Test = "nil", "Test"}
local count = 0

for index, value in table do
    count += 1
end

print(count) -- outputs 2.

Use table.getn(), if you want to be cool. Example:

local example = {}
print(table.getn(example)) -- 0

Actually, its easier than that. Since we just want to know if it’s empty, we don’t need to count them.

if not next(dictionary) then
    -- table empty
end
6 Likes