Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Delete a certain type of child from every part in a model?

Asked by
Jo_Bot 67
6 years ago

I want to be able to destroy a mesh from everything in a model, and I find it inefficient to erase the mesh manually by doing each part individually. Is there any way I can do it in 1 line, or a few lines? The meshes are all named the same thing.

0
script.Parent:GetDescendent(), try looking up how to do this. Apologies if I can't get into more detail, I'm extremely tired rn. Char187 0 — 6y

2 answers

Log in to vote
0
Answered by
zblox164 531 Moderation Voter
6 years ago

This will work.

EXAMPLE:

local Model = game.LocationOfWhereTheModelIs.Model:GetChildren()

for i, v in pairs(Model) do
    if v:IsA(Mesh) or v.Name == "Name of the part" then
        v:Destroy()
    else
        return false
    end
end

You can change the or in the if statement to and if you want both conditions to be true to continue with the code.

Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You can use a for loop, conditional statements, and :GetDescendants() like what Char187 said to destroy certain objects in your model.

Here is how you would do it :

local model = game.Workspace.SomeModel

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

So as you can see, the script is getting everything under the "model" with the for loop and :GetDescendants(). The script finds anything named "ModelIWantToDestroy" and destroys it. If you have multiple meshes with different names though, you can instead use the statement if v:IsA("BaseMesh") then, which find anything in the model that is a mesh (There are multiple types of meshes, so I put "BaseMesh". I don’t know if that exists though).

Hoped this helped, and sorry if my code explanation is a little long. :P

Answer this question