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!