Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

3 comments

0

Thank you for the help! I chose yours because you answer just happened to be simplest with my current knowledge. Plus detail.

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.

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?

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

1 comment

0

Thanks. Same as nicemike, you've helped me learn a little more on a subject I wasn't practiced in.

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)

1 comment

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.

This discussion is preserved as it was. New answers, comments, and votes are closed.