Delete all objects in a table

Hello. Im making a bomb that will delete anything that touches the hitbox. The problem comes because i use hitbox:GetTouchingParts() which is a table value and i cant do GetTouchingParts():Destroy because its a table value. Heres my script. Op is a circle part that is unanchored and deletes and explodes after a bit.

local hitbox = Instance.new("Part", Workspace)
local parttouched = hitbox:GetTouchingParts()
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Position = op.Position
hitbox.Size = Vector3.new(10, 10, 10)
op:Destroy()
parttouched:Destroy()
task.wait(1)
hitbox:Destroy()

I deleted some lines that isnt relevant now. Any help is appreciated

You should probably replace parttouched:Destroy() with this:

for _, part in parttouched do
    part:Destroy()
end

EDIT: Oh, also, the built-in Workspace variable is deprecated, so you should probably change that to workspace.

2 Likes

like @Rescriptedd said your parttouched variable is actually an array of objects which are currently touching the hitbox, so you can loop through it and destroy each part.

1 Like

Sorry for the late reply, but do i need to change out part for something else? I will put the part in brackets like (part) so you see what i mean

for _, (part) in parttouched do
    (part):Destroy()
end

This is the right methodology but you don’t need any parenthesis for this


for _, part in parttouched do
    part:Destroy()
end

If it works mark @Rescriptedd‘s post as the solution

No, you can name it whatever you want actually. Just make sure part:Destroy() is the name after the for _, with the :Destroy() of course.

Here’s the code for an example:

local hitbox = Instance.new("Part", workspace)
local parttouched = hitbox:GetTouchingParts()
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Position = op.Position
hitbox.Size = Vector3.new(10, 10, 10)
op:Destroy()
for _, part in parttouched do -- "part" can be anything, just make sure part:Destroy() is the name followed with :Destroy()
    part:Destroy() -- destroys all the parts found inside the hitbox
end
task.wait(1)
hitbox:Destroy()

Thanks! Also an extra detail. I dont really want the baseplate to be deleted too, is there any way to not delete the baseplate?

Really easy solution:

for _, part in parttouched do
    if part.Name == "Baseplate" then continue end
    part:Destroy()
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.