How to figure out if a table has values or not?

Hey Devs,

I believe that my title may be slightly misleading, as I know that I can print the table to figure out if it has values or not.

What I am really looking, and what I could figure out how to put into words for the title is this:

I datastore values to track player inventory. On loading these saves, they are loading into their own tables, and the tables use table.foreach to handle the value changes on the server. Here is an example of some of the code, and a tab that goes into further detail if you are interested:

– Code Sample

Summary

`
local effects = {}
table.insert(effects, stored[8])
table.foreach(effects, function(_,name)
local item = effectsinventory:FindFirstChild(name)
if item then
item.Value = true
else
local bool = Instance.new(“BoolValue”)
bool.Name = name
bool.Value = true
bool.Parent = effectsinventory
end
end)

– Breakdown

Summary

The values are loaded then added to their own tables. The “Values” are just the names of items that the player has purchases during prior play sessions, and the Values only save after a purchase. Only loaded in, the values are run through the code above, and they find the Value of the same name within the management folders, which set them to true.

Once the value changes, a remote event is triggered, and the item is added to the inventory GUI and the Shop GUI is changed so the player cannot repurchase the item.

Now here is the issue that I am facing:

This code currently works as long as the player has bought something of each of the shop categories; For instance, Skins, Effects and Animations. If the player has not bought an animation, then whenever the their data is loaded back up; the table.foreach function still runs, even through there are no values within the table.

The function still runs and it creates a blank value as you can see within the code sample. When this value is created, my scripts still attempt to run the remote events and do the GUI and Inventory changes for a nil object, which as you can assume, causes a lot of errors.

What I have tried so far:

I have searched around a lot, but cannot really find anything about checking a table for what may be inside of it. A lot of what I have found is table.find which only searches for a specific object, and people saying to use if effects:GetChildren() > 0 to check; but this overall just does not work for tables.

I may have overlooked some information somewhere, but this issue has had me stuck for the last day, which is why I decided to make this post.

To sum it up:

I basically just need a way of checking if a table has anything inside of it, and if it does not, then my function will not run.

If anyone could please point me to a post that has solved this issue, or just let me know of a way to index the table, then please feel free to reply with anything helpful. Thanks!

1 Like

For an array:

local tableHasValues = #table -- "table" would be the table

print(tableHasValues)

if it’s 0 then it has no values.

For dictionaries:

local values = 0

for _, _ in pairs(table) do -- like the array, "table" would be the table here
  values += 1
end

print(values)

If it stays at 0 then it also has no values.

Also, how does this not work? Do you check it before it receives any values?

2 Likes

So I have 8 different tables currently, so if you check the code sample in the original post; I would just be able to add:

if #effects ~= 0 then

and then follow it up with the rest of my code correct?

But as for the GetChildren():

I get the error: attempt to call a nil value.

I load the data in first and then check to make sure that it is actually loaded in using if loaded then. I then add it to a table, and then run the getchildren.

This is because :GetChildren() isn’t applicable with tables. It’s a member of Instance

2 Likes

This is probably due to the fact that GetChildren only works for Instances

Yes or if #effects > 0 then

1 Like

The most efficient way to check if a table is empty:

table1 = {}
table2 = {"test"}

function isEmpty(table)
    return next(table) == nil
end

print(isEmpty(table1)) -- true
print(isEmpty(table2)) -- false
4 Likes