Need solution for finding if instance exists

I need to find if an item exists but not via its name… an example of what I don’t want to use is findfirstchild.

  1. What do you want to achieve? Keep it simple and clear!
    I want to be able to check if certain subfolders exist inside of a folder. My script controls loading them in, removing them, adding them, and rotating them. If multiple of one are created it deletes any extra and saves it to the game.
  2. What is the issue? Include screenshots / videos if possible!
    The issue is that the for i, v in pairs I initially use to get all of the subfolders includes the extra folders I deleted. This errors and stops the script. Everything else works perfectly fine.
  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve tried to search up different ways to find if an item exists. An idea I’ve come up with is giving every subfolder a unique id that I could check for to see if it exists… but I’m not sure I should waste time making that if there is already a simple solution.

My subfolders are used to store data about timed developer product purchases from players…
Any help is appreciated! (this is my first ever post so I hope I did it right and feel free to give me criticism.)

What you are looking for is a traversal tree algorithm (I think that is the name). I suggest creating a function where if FindFirstChild(“Insert Name Here”) returns nil, then you should check to see if that child has other children. If it does, call the function itself and run the function on that folder/child.

OR

You can use :GetDescendant to get the entire children in an array and you can search to see if the folder you are looking for is contain in that folder.

i could use this, but what would i say if it was nil?

If it’s nil, it means the folder was not detected

Thing is my problem is that there are multiple subfolders of the same name. My script removes the duplicates but still recognizes the duplicates because it is all done within a for i, v in pairs.

for i, v in pairs(folder)
if subfolder duplicated the delete all other subfolders but v
end

the deleted folders are also checked even though they are already removed breaking the script

the deleted folders are also checked even though they are already removed breaking the script

pairs doesn’t guarantee it won’t iterate over nil items. It’s generally bad practice to loop over an array while at the same time adding/removing its items, and leads to behavior like this.

This StackOverflow link may help.

1 Like

so this? example:FindFirstChild()
if nil then
print(“hello”)
end

Item = Group:FindFirstChild(“Item”)
if Item then

End

Ty this helps letter letter

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