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

How do you Delete File Type, not name?

Asked by 5 years ago

The title says it all. I want to delete a model, but the name varies so I don't want to put :Destory() for every possible outcome. Is there a way I can delete a file type, not the name of it? if i could do script.Parent.Model:Destory(), that would be nice! So is there anyway I could do that?

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

You can detect the ClassName of the object, which will find it regardless of the name:

local model = game.Workspace:FindFirstChildWhichIsA("Model") -- Model name does not matter
if model then
    model:Destroy()
end

As my comment explains, FindFirstChildWhichIsA() ignores the model's Name and instead finds the model directly by ClassName. Other methods that do this include IsA() and FindFirstChildOfClass().

Ad
Log in to vote
0
Answered by 5 years ago

This is a follow-up to MCAndRobloxUnited's answer. If you wanted to delete all of a certain instance in the workspace you could do this

local descendants = game.Workspace:GetDescendants()
for i=1, #descendants do
    if descendants[i]:IsA("InstanceTypeHere") then
        descendants[i]:Destroy()
    end
end

Answer this question