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)
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.
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
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)