How to detect a non-nil part in an array?

I have an array storing x parts, where many of these parts get destroyed. How can I get a part from this array that’s not nil, so that didn’t get destroyed?

I’ve tryed to remove the parts from the array whenever they get destroyed, and it works fine, but I’m curious to know how to get a non nil part from the array.
Thanks in advance!

to check if it exists:

if table[x] then

or

for _, v in pairs(table) do
if v then
--code 
end
end
30 char 30 char
1 Like

I’ve tried this yet, but it throws error everytime I try to get it’s parent

I don’t think it’s going to work, since it’s the same thing as the one above. But I’ll try it!
Anyway thanks for the help!

what is the code you are using?
if something is nil, you cant find the parent of nil.

Here’s the deal, so if you index a part that you destroy that’s also stored in an array, like so-

print(table[x])

Even if the physical instance of the brick is destroyed, that should still print the object itself. This is because the part is still being referenced by the table directly, and therefor that object is still in memory, although the parent of the object has been locked and set to nil, as is what :Destroyed() does when it’s called.

So with that being said, just check to see if its parent is set, like so:

if table[x].Parent then

print'exists'

end
1 Like

It works! Thanks!

30characters

1 Like