Checking if a table is empty

I have a script that invokes a function with a remote function and receives 2 tables back. The output typically looks like this:
image

There are instances, however, where one of the tables can be blank. I’ve tried checking that with an if statement, but it hasn’t worked at all. I have confirmed that the table is empty as well.

These are the methods I’ve tried:

if Result[1] == {} then
if Result[1] == nil then
if Result[1] then --I flipped the code for this one so it would work with a true check

Result is a variable that invokes the function and returns what was shown in the picture local Result = RS.Inventory.Split:InvokeServer(tonumber(Slot.Name),HeldItemPresent)

Assuming the tables have numerical indexes, you can check the amount of indexes the table has by putting a # before whatever variable you use to store it. So in your case, try:

if #Result[1] == 0 then
Note - This will return the amount of ordered index items there are, e.g if index 1 doesn’t exist, it will return 0, even if index 2,3,4… etc exist.

If they don’t have numerical indexes, then you should do a for loop and save a variable that increments by one for each item in the table, or setup a function that will return true if it finds something in the table.

The indexes are set up like this: [“Wheat Seeds”] = 19. Edit: Never mind. I’ll do the for loop thing

If you only want to know if the table is empty or not just know you don’t need to loop over the entire table instead try this:

local function isEmpty(t)
    for _, _ in t do
        return true
    end

    return false
end

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