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

How can I make sure all instances of an object get destroyed, despite heiarchy?

Asked by 8 years ago

Everything here works more or less how I want it to, except one thing. I can't figure out how to destroy all instances of W. The script's goal was to create a large ball that grabbed everything it touched and pulled it into it's center as soon as you stepped on a block. The problem is, after this vacuum dissipates, it leaves you stuck at it's used-to-be core. I just want all BodyPositions to be removed after the script finishes, yet I can't seem to figure out how.(I have low knowledge on scripting still and have been working on this single thing for 3 hours)

Debounce = false
block = script.Parent.Position

function touch(hit)
if Debounce == false then
    Debounce = true
    wait(5)
    local bh = Instance.new("Part", workspace)
    bh.Name = "bh"
local function touched(bop)
if bop.Anchored == false then
    local w = Instance.new("BodyPosition", bop)
    w.Name = "w"
if bop:findFirstChild("w") then
for i = 100, 1000, 10 do
    w.D = i
    w.P = i
    w.Position = block
    w.MaxForce = Vector3.new(i,i,i)
    wait(.05)
end
end
end
end
workspace:findFirstChild("bh").Touched:connect(touched)

if workspace:findFirstChild("bh") then  
    bh.Shape = 0
    bh.BrickColor = BrickColor.new(1003)
    bh.CFrame = CFrame.new(block)
    bh.Anchored = true
    bh.CanCollide = false
for i = 1, 50, 1 do
    bh.Size = Vector3.new(i,i,i)
    bh.CFrame = CFrame.new(block)
    wait(i/2000)
end
for i = 50, 1, -.5 do
    bh.Size = Vector3.new(i,i,i)
    bh.CFrame = CFrame.new(block)
    wait(i/6000)
end     
    bh:Destroy()
    Debounce = false
end
end
end
script.Parent.Touched:connect(touch)

3 answers

Log in to vote
0
Answered by
dyler3 1510 Moderation Voter
8 years ago

One simple way that you could do this would be by enabling FindFirstChild()'s second parameter, like so:

game.Workspace:FindFirstChild("w", true)

The second parameter is a boolean (true / false) value that, when true, makes the function search through every descendent of the defined instance, which in this case is Workspace.

By default, the second parameter is set to false, so it won't work without enabling it like this.

If no valid instance is found with the defined name, then the function returns nil.


Using this, we can create a while loop (or a repeat loop), and having it repeat until all instances have been deleted. That would look something like this:

while game.Workspace:FindFirstChild("w", true) do -- Searches every instance in Workspace
    game.Workspace:FindFirstChild("w", true):Destroy() --Destroys each part
end

Anyways, I hope this helps. If not, or if you have any further questions, please leave a comment below, and I'll see what I can do.

0
Thank you for the help! I chose yours because you answer just happened to be simplest with my current knowledge. Plus detail. pmcdonough 85 — 8y
0
There's a better way to do this, and what you provided doesn't really help. He's trying to find body mover created by the blackhole. The body movers aren't parented to workspace, which means he would have to use it in a for loop which checks all of the objects effected by the blackhole. aquathorn321 858 — 8y
0
I'm not really sure what you're talking about. This is the simplest way that you could possibly do this. And they are DESCENDANTS of Workspace, so this code works perfectly fine. Did you even read how I explained it? dyler3 1510 — 8y
Ad
Log in to vote
0
Answered by
Voltoxus 248 Moderation Voter
8 years ago

I would suggest using this function I wrote, here is an example:


function SearchAndDestroy(obj,targ) for j,k in pairs(obj:GetChildren())do if k.Name == targ then k:Destroy() end SearchAndDestroy(k, targ) end end SearchAndDestroy(game.Workspace, "Decal")

Code:

function SearchAndDestroy(obj,targ)
    for j,k in pairs(obj:GetChildren())do
        if k.Name == targ then
             k:Destroy()
        end
        SearchAndDestroy(k, targ)
    end
end
0
Thanks. Same as nicemike, you've helped me learn a little more on a subject I wasn't practiced in. pmcdonough 85 — 8y
Log in to vote
0
Answered by 8 years ago

Try this out. I commented some things, fixed the indentation (which is really important!), and added a way you could accomplish what you want. It's not the greatest method, but it should be okay for you to learn:

Debounce = false
block = script.Parent.Position

function touch(hit)
    if Debounce == false then
        Debounce = true
        wait(5)
        local bodyPositions = {} --lets keep track of them in a table shall we?
        local bh = Instance.new("Part", workspace)
        bh.Name = "bh"
        local function touched(bop)
            if bop.Anchored == false then
                local w = Instance.new("BodyPosition", bop)
                w.Name = "w"
                table.insert(bodyPositions, w) -- add this one to that table
                -- removed this statement because it's redundant (you just created w, no need to check): if bop:findFirstChild("w") then
                for i = 100, 1000, 10 do
                    w.D = i
                    w.P = i
                    w.Position = block
                    w.MaxForce = Vector3.new(i,i,i)
                    wait(.05)
                end
            end
        end
        bh.Touched:connect(touched) -- modified; you don't need to re-find bh since you already have the variable for it

        -- again no need to check. Removed: if workspace:findFirstChild("bh") then  
        bh.Shape = 0
        bh.BrickColor = BrickColor.new(1003)
        bh.CFrame = CFrame.new(block)
        bh.Anchored = true
        bh.CanCollide = false
        for i = 1, 50, 1 do
            bh.Size = Vector3.new(i,i,i)
            bh.CFrame = CFrame.new(block)
            wait(i/2000) -- this function will, at minimum, wait 1/30 of a second, or wait()
        end
        for i = 50, 1, -.5 do
            bh.Size = Vector3.new(i,i,i)
            bh.CFrame = CFrame.new(block)
            wait(i/6000) -- see above
        end
        -- remove everything in the table:
        for i, v in pairs(bodyPositions) do -- if you need explanation on this line, or more about tables, just ask
            if v then -- in case something got removed prematurely somehow
                v:Destroy()
            end
        end
        bh:Destroy()
        Debounce = false -- I also think this debounce used to be inside that 'if' statement, when it shouldn't have been
    end
end
script.Parent.Touched:connect(touch)
0
Thank you too, your answer helped as well, and basically told me that I need to start using i, v- something I was ignorantly avoiding. pmcdonough 85 — 8y

Answer this question