How do I destroy a part?

Basically when the car touches the mine, the mine explodes and the entire car unanchors then flies all over the place.
I’m trying to make the car parts that are laying all over the place disappear after a certain amount of time.

here is my code, this is just a piece of the main code but I want the part to destroy after about 10 seconds or so.

local jeep = game.Workspace:WaitForChild("Jeep")

boom.Hit:Connect(function(part: BasePart, _)
			part.Anchored = (part == mine) or false
			
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent.Humanoid.Health = 0
			end;
			task.wait(5)
			Jeep:Destroy() -- trying to make the part that got unanchored destroy(not working)
		end)
4 Likes

Is there any errors in the output ?

No, there are none?

I want to make sure the parts that get unanchored get destroyed, how can I do that?

Here is the entire script:

local mine = script.Parent
local sound = script.Parent.BoomSound
local DEBOUNCE = false
mine.Transparency = .9

mine.Touched:Connect(function(hit)
	local minePosition = mine.CFrame.Position
	
	if hit:IsA("Terrain") or hit:IsA("Foggy Wind") then
		return;
	end;
	
	if not DEBOUNCE then
		DEBOUNCE = true
		
		sound:Play()
		local boom = Instance.new("Explosion")
		boom.ExplosionType = Enum.ExplosionType.NoCraters
		boom.BlastPressure = 500000
		boom.BlastRadius = 10
		boom.Parent = mine
		boom.Position = minePosition
		boom.Visible = true
		
		boom.Hit:Connect(function(part: BasePart, _)
			part.Anchored = (part == mine) or false
			
			if hit.Parent:FindFirstChild("Humanoid") then
				hit.Parent.Humanoid.Health = 0
			end;
			
		end)
		
		local light = Instance.new("PointLight")
		light.Parent = mine
		light.Brightness = 10
		light.Range = 12
		light.Color = Color3.new(0.942306, 0.0528725, 0)
		light.Enabled = false
		
		task.wait(0.4)
		
		light.Enabled = false
		
		mine.Transparency = 1
		
		sound.Ended:Wait()
		
		wait(5)
		
		mine.Transparency = 0.9
		
		DEBOUNCE = false
	end
end)
1 Like

I think its because you have typed “J” with a capital, you haven’t really specified what you wanna destroy on line 10 “Jeep:Destroy()”

5 Likes

Well, there are multiple jeeps in game, how do I specify the one that touched it?

The first parameter is the object that touched the explosion

Try this

local jeep = game.Workspace:WaitForChild(“Jeep”)

boom.Hit:Connect(function(PartTouched)
part.Anchored = (part == mine) or false

  	if hit.Parent:FindFirstChild("Humanoid") then
  		hit.Parent.Humanoid.Health = 0
  	end;
  	task.wait(5)
  	PartTouched:Destroy() -- trying to make the part that got unanchored destroy(not working)
  end)

what about something like this?
I dont know how to do it correctly

local car = part:FindFirstAncestor("jeep")
local carChild = car:GetChildren()
carChild:Destroy()
2 Likes

Ill show you a video of what im talking about.

I’m trying to get rid of these parts.
https://gyazo.com/71836a2ec3282646a8e06076fc9ba3c0

2 Likes

That just destroys one part, I need to get rid of the entire car so it doesnt lag the game

2 Likes

can i see the car in explorer and how its setup?

Screenshot 2023-07-08 125131

2 Likes

Try this one too, I chande “Jeep” with “hit”

you could try to use a for loop to add every part to a debris function which will delete the part after a certain period of time for example

for index, part in jeep:GetChildren() do
    Debris:AddItem(part, [amount of time you want to take])
end

I havent tried it before but see how it works out for you

(im not sure why my indent isnt working)

3 Likes

If the jeep model is the first ancestor of all the parts you can just do Hit.Parent:Destroy(), you can also use a loop that goes up a few parents until it reaches Jeep. The last option could be naming the jeeps based on who is in them, and using a player name against it.

Okay, with the loop then, where do I start?

1 Like

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