Deleting instances with certain names

Hello, I am having an issue with removing parts in workspace named “SelectionBox”.

This is what I have tried so far:

local box = workspace:FindFirstChild("SelectionBox")


if box 


then
box:Remove()
end

This did not work. These parts are grouped into different models with different names, so how would I accomplish this?

Do they have a common parent?

I would find a common parent and either use :GetChildren() or :GetDescendants() to loop through and find all the instances names “SelectionBox”

for i,v in pairs (CommonParent:GetDescendants()) do
      if v.Name == "SelectionBox" then
            v:Destroy()
      end
end

:Remove() is deprecated, use :Destroy() instead. With how your code is right now, if theres an object named “SelectionBox” under workspace then it would get destroyed, are you sure you have the right parent?

1 Like

The parts are all in the same groups but some of the groups are parented to other groups.

They are all under a group but some of the groups are parented to other/multiple groups.

Write a function that iterates through all the instances in the workspace(I assume they are in workspace) and deletes them.

If that’s inefficient.

Use Collection Service, tag them, and and delete them when you want to delete them.

1 Like

Just loop through all parts (Note: if you have a lot of parts this will take a while and might result into lag, you can always use collection service)

for i,v in pairs (workspace:GetDescendants()) do
      if v.Name == "SelectionBox" then
            v:Destroy()
      end
end
2 Likes

Try this:

local Box = workspace:FindFirstChild('SelectionBox', true)

if Box then
  Box:Destroy()
end
1 Like

You’ll need to loop through the workspace to find everything that shares that name.
Here’s an example using ipairs (since :GetDescendants() returns an array):

local Workspace = game:GetService('Workspace')
for Index, Part in ipairs(Workspace:GetDescendants()) do
   if Part.Name == 'Name' then
      Part:Destroy()
   end
end
1 Like


Trying it later, are you experiencing this problem? Roblox is mistaking properties for children. This happens on a lot of my scripts and forces me to make unnecessary variables for things like “script.Parent”

No, I am not experiencing your issue, never have.
And, I’m pretty sure you get that error because models doesn’t have a Transparency property, check if it’s a basepart before setting it.