Delete other parts based on name

Hello i was wondering if it’s possible to make a script inside of a part that only deletes other parts (when touched by the part with the script in it) if their name matches the criteria in the script?

if it is, would it be possible to show me how to do so in the comments?

It is possible,

local specifiedName = "Name"

script.Parent.Touched:Connect(function()
   for _, v in pairs (game.Workspace:GetDescendants()) do
     if v ~= script.Parent then
      if v.Name == specifiedName then
         v:Destroy()
      end
    end
  end
end)
2 Likes

thanks a lot, ill test this right now.

1 Like

for some reason when i tested it, the part deleted all of the other parts with the specified name. do you know why?

1 Like

You said to do nothing or delete? Cause I made it delete the parts with the specified name.

1 Like

it deleted all of them, but i wanted it to only delete them when it touches the named parts

1 Like

Alright.

  local touchConns = { }

   for _, v in pairs (game.Workspace:GetDescendants()) do
     if v ~= script.Parent then
      if v.Name == specifiedName then
         
             touchConns[#touchConns+1] = v.Touched:Connect(function()
                   v:Destroy()
             end)

      end
    end
  end

Sorry about the formatting

2 Likes
local part = script.Parent

part.Touched:Connect(function(hit)
	if hit.Name == "" then --some name
		hit:Destroy()
	end
end)

Code needs to be inside a server script, parent the script to the main part (the one which causes other parts to be deleted).

2 Likes

To remove AnotherPart by simply clicking on the first part.

local Name = game.Workspace.Name

script.parent.clickdetector.mouseclick:Connect(function(player)
  Name:Destroy()
end()

The code can be put in the first Part.

2 Likes

The original poster was looking for a script which deletes parts of a specified name if they come into contact with some other part.

4 Likes

They didn’t specify, so I just put in my script if hit.Name == "" so they can change the name inside the quotes.

3 Likes

What do you mean by this, how do i do that?

the problem with this one is that even when the player touches the “specified part” it destroys (deletes) , how would i make it so that only the part with the script inside can delete the “specified parts”

Just copy and paste the code I provided into a script (not a local script) and then place the script inside the part.

Thanks alot : ) . I hope this works…

Don’t forget that you’ll need to change what’s inside the quotes to match the name of the parts you want this to work on.

2 Likes

ok, ill remember that. thanks a lot

I did this but it has a very long delay for what i want to use it for…

Edit: ill upload the vid somehow: Grasscutting GIF | Gfycat

just to prove that it isn’t lag i set the graphics to the lowest.

is there a way that i can fix the delay?

Can you place all of the parts (all of the grass) inside of a folder? Then place this script inside of the folder.

local Parts = script.Parent:GetChildren()

for I,v in pairs(Parts) do
   v.Touched:Connect(function()
     v:Destroy()
   end)
end

But, id like to do it so that only a certain part can “cut” the grass