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

Plugin search through EVERY descendant of EVERY descendant and removing certain names?

Asked by 8 years ago

What I've done is made ONE plugin that makes the shadows into descendants, but I don't know how to remove them again...

Making

--That button and Toolbar stuff
button.Click:connect(function()
button.Click:connect(function()
A = game.Workspace:GetChildren()
for i,v in pairs(A) do
    if v.ClassName == "Model" then
        human = Instance.new("Humanoid") 
        human.Parent = v
        human.Name = "Shadow"
        human.DisplayDistanceType = "None"
    elseif v.ClassName == "Union" then
        human = Instance.new("Humanoid") 
        human.Parent = v
        human.Name = "Shadow"
        human.DisplayDistanceType = "None"
    end
end 
end)

My attempt at removing

--Toolbar and Button stuff
button.Click:connect(function()
A = game.Workspace:GetChildren()
for i,v in pairs(A) do
    if v.ClassName == "Humanoid" and v.Name == "Shadow" then
        v:remove()
    end
end 
end)

Note The Maker does not also go through each descendant, which I also need help with.

1 answer

Log in to vote
1
Answered by 8 years ago

Based on your orignal code you're adding 'Shadows' to models and unions who are children of workspace, but I see you want to apply this to all descendants of workspace. An easy way to do that is to write a recursive function.

button.Click:Connect(function()
    function addShadow(obj)
        for i,v in pairs(obj:GetChildren())
            if v:IsA("Union") or v:IsA("Model") then
                local human = Instance.new("Humanoid") 
                 human.Name = "Shadow"
                 human.DisplayDistanceType = "None"
                 human.Parent = v
            end
            addShadow(v) --function calls itself on children of obj making it recursive.
        end
    end 
    addShadow(workspace)    
end) 
-- and then to remove what we added
button.Click:Connect(function()
    function removeShadow(obj)
        for i,v in pairs(obj:GetChildren())
            if v.Name == "Shadow" and v:IsA("Humanoid") then
                v:Destroy()
            end
            removeShadow(v) 
        end
    end
    removeShadow(workspace)     
end) 

Ad

Answer this question