Script remembers destroyed instances

I have a simple script that places parts and explodes them, but parts that exploded before explodes again when destroyed.
like this:


my script:

script.Parent.Place.OnServerEvent:Connect(function(player, mousePosition)
	local part = Instance.new("Part")
	part.Position = mousePosition
	part.Parent = game.Workspace
			script.Parent.BOOM.OnServerEvent:Connect(function()
			local explosion = Instance.new("Explosion")
			explosion.Position = part.Position
			explosion.Parent = game.Workspace
			part:Destroy()
	
		end)
end)

When you destroy the part, you don’t destroy the explosion. To fix this, parent the Explosion to the part so that when the part is destroyed, all of the children including the Explosion will be destroyed.

1 Like

this works but because it destroys the part after explosion, explosion is destroyed immediately.
so I tried this but it doesn’t destroyst the part at all

explosion.Destroying:Connect(function()
	part:Destroy()
end)
1 Like

I dont quite understand what you mean by the explosion is destroyed

Also, could you try parenting the Explosion to the part and see what happens using your original code:

explosion.Parent = part
1 Like

When I parent it to the part I can’t see the explosion, It is destroyed with the part immediately

1 Like

Ohhhh right, that happens. Set the parent back to workspace and try the following code after you destroy the part:

wait(3)
explosion:Destroy() --wait 3 seconds and then destroy the explosion instance manually
1 Like

It still doesn’t work.
here is the code if i missed anything

script.Parent.Place.OnServerEvent:Connect(function(player, mousePosition)
	local part = Instance.new("Part")
	part.Position = mousePosition
	part.Parent = game.Workspace
	
		script.Parent.BOOM.OnServerEvent:Connect(function()
			local explosion = Instance.new("Explosion")
			explosion.Position = part.Position
			explosion.Parent = game.Workspace
			part:Destroy()
			wait(3)
			explosion:Destroy()
		end)
end)

thank you for your time though

1 Like

If its okay with you, could I get some more background information on how the BOOM and Place events work? Thanks.

1 Like

Place event

local mouse = game.Players.LocalPlayer:GetMouse()


script.Parent.Activated:Connect(function()
	script.Parent.Place:FireServer(mouse.Hit.p)
end)
	

firing it when tool activated,

boom event

local uis = game:GetService("UserInputService")

uis.InputBegan:Connect(function(input)
	
	if (uis:GetFocusedTextBox()) then
		return;
	end
	if input.KeyCode == Enum.KeyCode.Q then
		script.Parent.BOOM:FireServer()
	end	
end)

just firing itwhen q key pressed

1 Like

Could you try altering the scripts and instead, add a script inside the part and instead of adding a new instance, could you try cloning an already-made part with this script inside it:

(Put this script inside a Part object in ServerStorage)

"where ever the event is located".BOOM.OnServerEvent:Connect(function()
			local explosion = Instance.new("Explosion")
			explosion.Position = script.Parent.Position
			explosion.Parent = game.Workspace
			script.Parent:Destroy()
		end)

(Change the original script to:)

script.Parent.Place.OnServerEvent:Connect(function(player, mousePosition)
	local part = game.ServerStorage:FindFirstChild("Part"):Clone()
	part.Position = mousePosition
	part.Parent = game.Workspace
end)```
1 Like

You connect the event every time you place a part and then dont disconnect it.
Try this:

script.Parent.Place.OnServerEvent:Connect(function(player, mousePosition)
	local part = Instance.new("Part")
	part.Position = mousePosition
	part.Parent = game.Workspace

    local connection

	connection = script.Parent.BOOM.OnServerEvent:Connect(function()
		local explosion = Instance.new("Explosion")
		explosion.Position = part.Position
		explosion.Parent = game.Workspace
		part:Destroy()
        connection:Disconnect()
	end)
end)

Or you can just put the BOOM event outside of the place event, and then loop through all parts you want to explode:

local partsToExplode = {}


script.Parent.Place.OnServerEvent:Connect(function(player, mousePosition)
	local part = Instance.new("Part")
	part.Position = mousePosition
	part.Parent = game.Workspace

    table.insert(partsToExplode, part)
end)


script.Parent.BOOM.OnServerEvent:Connect(function()
    for _, part in pairs(partsToExplode) do
		local explosion = Instance.new("Explosion")
		explosion.Position = part.Position
		explosion.Parent = game.Workspace
		part:Destroy()
    end

    partsToExplode = {}
end)

The second method would probably be better, since you would have less connections

2 Likes

Oh thank you, that was the problem.

2 Likes