How can I make parts that are touched by an explosion disappear after 5 seconds? (Not solved)

I have this script, Of a rocket launcher:

    	if ExplodeEvent then
		Rocket.Touched:Connect(function(TouchPart)
			if not IGNORE_LIST[string.lower(TouchPart.Name)] and not TouchPart:IsDescendantOf(Character) then
				ExplodeEvent:FireServer(Rocket.Position)
				if callFunction == false then
					callFunction = true
					LocalTouchSound:play()
					callFunction = false
				end
			end
		end
	)end
end

And I want to make that every part the explosion of the rocket launcher touches, Will disappear after 5 seconds.
Can someone help me please/

1 Like

Well, The explosion object has a hit event that you could use for this purpose, coupled with the delay function, you can make something like this:

Explosion.Hit:Connect(function(part,distance)
   delay(5,function()
      part:Destroy()
   end)
end)

Explosion object API referece

3 Likes

I tried this, But it only destroys one part, The first part the rocket touches, Even though much more parts are falling out of the building.

Interesting, when i tried this out, multiple parts were detecteddemo

I would suggest modifying the BlastRadius, as that determines what parts are detected by the Hit Event.

The blast radius is fine, Maybe I’m not putting the function in the right place…
Here is the full script:

it appears that you are using Rocket.Touched, as opposed to Explosion.Hit, which gets parts touched by the rocket, rather than the explosion, furthermore, you are destroying parts in a localscript, which would not replicate to the server

There’s also a server script inside the rocket launcher…

local Tool = script.Parent
local Handle = Tool:WaitForChild("Handle")
local EquipSound = Handle:WaitForChild("EquipSound")
local ReloadSound = Handle:WaitForChild("ReloadSound")

local Resources = require(script.Parent:WaitForChild("Resources"))
local RocketCreator = require(Resources:GetResource("RocketCreator"))
local BufferCreator = require(Resources:GetResource("BufferCreator"))
local RemoteEventCreator = require(Resources:GetResource("RemoteEventCreator"))
local Configuration = require(Resources:GetResource("Configuration"))

local ROCKET_RELOAD_TIME = Configuration.ROCKET_RELOAD_TIME

local RocketBuffer = BufferCreator:CreateServerBuffer("RocketBuffer")
local FireRocketEvent = RemoteEventCreator:CreateRemoteEvent("FireRocket")

local CurrentPlayer,CurrentCharacter
local CurrentHandRocket,CurrentRocket
local Equipped = false
local ToolEnabled = true



--Adds a rocket to the buffer.
local function AddBufferRocket()
	local NewRocket = RocketCreator:CreateRocket(true)
	CurrentRocket = NewRocket
	RocketBuffer:AddItem(NewRocket)
end
AddBufferRocket()




--Set up firing the rocket.
FireRocketEvent.OnServerEvent:Connect(function(Player)
	if Player == CurrentPlayer and ToolEnabled then
		ToolEnabled = false
		Tool.Enabled = false
		CurrentRocket = RocketBuffer:PopItem() or CurrentRocket
		
		local FiredBy = Instance.new("ObjectValue")
		FiredBy.Name = "FiredBy"
		FiredBy.Value = Player
		FiredBy.Parent = CurrentRocket
		
		wait(ROCKET_RELOAD_TIME)
		AddBufferRocket()
		ToolEnabled = true
		Tool.Enabled = true
	end
end)

--Set up equipping and unequipping the tool.
Tool.Equipped:Connect(function()
	if not Equipped then
		Equipped = true
		
		CurrentCharacter = Tool.Parent
		CurrentPlayer = game.Players:GetPlayerFromCharacter(CurrentCharacter)
		EquipSound:Play()
		
		--Create the left hand rocket.
		local LeftGripAttachment = CurrentCharacter:FindFirstChild("LeftGripAttachment",true)
		if LeftGripAttachment then
			CurrentHandRocket = RocketCreator:CreateRocket(false)
			CurrentHandRocket.Name = "HandRocket"
			CurrentHandRocket.Transparency  = 1
			CurrentHandRocket.Parent = Tool
			
			local Weld = Instance.new("Weld")
			Weld.Part0 = LeftGripAttachment.Parent
			Weld.Part1 = CurrentHandRocket
			Weld.C0 = LeftGripAttachment.CFrame
			Weld.C1 = CFrame.Angles(0,math.pi,0)
			Weld.Parent = CurrentHandRocket
		end
		
		RocketBuffer:SetCurrentPlayer(CurrentPlayer)
	end
end)

Tool.Unequipped:Connect(function()
	if Equipped then
		Equipped = false
		
		CurrentCharacter = nil
		CurrentPlayer = nil
		EquipSound:Stop()
		if CurrentHandRocket then CurrentHandRocket:Destroy() end
		RocketBuffer:SetCurrentPlayer()
	end
end)

Maybe I should put it somewhere here?

Another method you could use is Debris, works the same as Destroy but it doesn’t yield:

local debris = game:GetService("Debris");

Explosion.Hit:Connect(function(part,distance)
debris:AddItem(part, 7);
end)

It says “Unknown global exploshion”…
should I use “Rocket” instead?

It was just an example, adjust it to your script accordingly.

Well, I tried it with rocket on the local script, Still doesn’t work…

Assuming you copied the error word for word, exploshion should be explosion

Yes, That what I mean - explosion****

I’m still having problem with it… i tried many more ways to make the parts disappear… Nothing works

Where you make the explosion, connect a function to remove part to the .hit event of that specific explosion

What you could do, rather than listening to touch events if they for whatever reason do not work, is use region3 to collect all the parts in a certain radius once the explosion explodes

untested pseudocode;

function rocketExploded()
      local position = rocket.Position
      local point1 = position-Vector3.new(5,5,5)
      local point2 = position+Vector3.new(5,5,5)
      local region = Region3.new(point1,point2)
      local parts = workspace:FindPartsInRegion3(region)

      for _, part in pairs(parts) do
          game:GetService("Debris"):AddItem(part,5)
      end
end

I tried making hit event with the rocket…
Not working.

The first one, is “local function rocketExploded()”? Or without local?

It doesn’t really matter with functions unless they are within other functions. I would personally put it as local function rocketExploded() but I know some developers prefer not to.

It seems to work, But very laggy and it makes the parts stuck in the air, then just disappear…
How can I add a delay or wait to that function? (between rocket shots)