How would I remove a part if it touches another part?

I have two parts, one at the bottom of a hill and another part that spawns in at a 1 second increment.

This is the part I want to delete the parts that spawn when touched. (Named, DeleteEggs)

Here is the script that spawns the eggs at the top of the hill.


local EggFolder = Instance.new("Folder")
EggFolder.Name = "EggFolder"
EggFolder.Parent = game.Workspace
wait(1)

local Part = game.Workspace:WaitForChild("EggCreator")
local EggTemplate = game.ReplicatedStorage:WaitForChild("Templates").EggTemplate

local function spawnEgg()
	local placeholderParent = Instance.new("Part")
	placeholderParent.Size = Vector3.new(2.26, 1.97, 2.06)
	placeholderParent.Parent = workspace.EggFolder
	placeholderParent.Transparency = 0
	placeholderParent.Anchored = false
	placeholderParent.Name = "EggKiller"
	
	local random1 = math.random(-Part.Size.X,Part.Size.X)/2
	local random2 = math.random(-Part.Size.Z,Part.Size.Z)/2
	placeholderParent.Position = Part.Position+Vector3.new(random1,Part.Size.Y/2,random2)
	
	local placeholder = Instance.new("SpecialMesh")
	placeholder.Parent = placeholderParent
	placeholder.Scale = Vector3.new(1,1,1)
	placeholder.MeshId = "rbxassetid://379909978"
end

while wait(1) do
	spawnEgg()
end

Now here is what I have currently that deletes that eggs that roll down the hill. (Which doesnt work.)

wait(1)
local DeleteEgg = game.Workspace.DeleteEggs
local Egg = game.Workspace.EggFolder.Egg

game.Workspace.DeleteEgg.Touched:Connect(function(partTouching)
	if Egg == partTouching then
		Egg.Remove()
	end
end)

Now, first off. This doesnt work for one reason that I know, that the egg would only delete ONE egg, not all the eggs that spawn. This is because the script is not looping looking for new parts. (Like that I have in a script that kills the player with the egg).

Here is that by the way*

wait(1)
local folder = workspace.EggFolder

--local debounce = true

while true do
	wait(1)
	for _,v in pairs(folder:GetChildren()) do
		if v:IsA('BasePart') then
			v.Touched:Connect(function(touched)
				--if debounce == true then
					--debounce = false
					local possibleHumanoid = touched.Parent:FindFirstChildWhichIsA('Humanoid')
					if possibleHumanoid then
					possibleHumanoid.Health = 0
					v:Destroy()
					end

				---end
			end)
		end
	end
end

How would I be able to prevent this from happening by changing the script that deletes it because the other scripts work perfectly!

2 Likes

I decided to make a rewrite of the kill script. Here you go:

local folder = workspace:WaitForChild("EggFolder")

folder.DescendantAdded:Connect(function(descendant)
	if descendant:IsA("BasePart") then
		descendant.Touched:Connect(function(touched)
			local humanoid = touched.Parent:FindFirstChildWhichIsA("Humanoid")
			if humanoid then
				humanoid.Health = 0
			descendant:Destroy()
		end)
	end
end)

And, for the other one, you used Egg.Remove(), which doesn’t exist at all. Instead, use Egg:Destroy()

Hi!
I did notice that I accidentally types Egg.Remove() instead of Egg.Destroy()

I did fix that and it still doesnt work.

Did you type Egg.Destroy() or Egg:Destroy() ?

wait(1)
local DeleteEgg = game.Workspace.DeleteEggs
local Egg = game.Workspace.EggFolder.Egg

game.Workspace.DeleteEgg.Touched:Connect(function(partTouching)
	if Egg == partTouching then 
		Egg:Destroy()
	end
end)

1 Like

Your script will only work for one egg. Try this instead:

local DeleteEgg = workspace:WaitForChild("DeleteEggs")
local EggFolder = workspace:WaitForChild("EggFolder")

game.Workspace.DeleteEgg.Touched:Connect(function(partTouching)
	for _, Egg in pairs(EggFolder:GetChildren()) do
		if Egg == partTouching then
			Egg:Destroy()
		end
	end
end)

Edit: Yes, I had to edit the script since I made a string instead of a table in pairs().

You need to use your variable DeleteEgg instead of “game.Workspace.DeleteEgg” as your part is named “DeleteEggs”.

In that case, here’s an even BIGGER rewrite:

local DeleteEgg = workspace:WaitForChild("DeleteEggs")
local EggFolder = workspace:WaitForChild("EggFolder")

DeleteEgg.DescendantAdded:Connect(function(descendant)
	descendant.Touched:Connect(function(partTouching)
		if table.find(EggFolder:GetChildren(), partTouching) then
			partTouching:Destroy()
		end
	end)
end)

Note: Variable DeleteEgg must be a Folder or Model.

Edit: I changed the script to be cleaner.

The “DeleteEggs” is the actual part, so would I make another folder for that?

Should be:

wait(1)
local DeleteEgg = game.Workspace.DeleteEggs
local Egg = game.Workspace.EggFolder.Egg

game.Workspace.DeleteEgg.Touched:Connect(function(partTouching)
	if partTouching.Name == "--Part that you want it to touch name" then
		Egg.Destroy()
	end
end)

I’m assuming I would loop this so it can find any new spawned part with the name?

.Remove() does exist, it’s just that the syntax is not correct and Instance:Remove() is deprecated (but it’s not according to the object browser???)

Unnecessary, you could just do:

wait(1)

local DeleteEgg = game.Workspace.DeleteEgg
local Egg = game.Workspace.EggFolder.Egg

DeleteEgg.Touched:Connect(function(touchedPart)
	if touchedPart.Name == "Egg" then
		touchedPart:Destroy()
	end
end)

Edit: Cleaned script up a bit.

wait(1)
local DeleteEgg = game.Workspace.DeleteEggs
local Egg = game.Workspace.EggFolder.EggKiller

while true do
	wait(1)
	game.Workspace.DeleteEgg.Touched:Connect(function(partTouching)
		if partTouching.Name == DeleteEgg then
			Egg.Destroy()
		end
	end)
end

Got this error? The part is very clearly in there.

16:34:53.186 EggKiller is not a valid member of Folder “Workspace.EggFolder” - Server - RemoveEggs:3
16:34:53.186 Stack Begin - Studio
16:34:53.186 Script ‘ServerScriptService.RemoveEggs’, Line 3 - Studio - RemoveEggs:3
16:34:53.186 Stack End - Studio

You need to wait for the egg killer and since there’s multiple egg killers:


workspace.EggFolder.ChildAdded:Connect(function(Egg)
	Egg.Touched:Connect(function(partTouching)
		if partTouching.Name == "DeleteEgg" then
			Egg:Destroy()
		end
	end)
end)

I added this in, doesnt delete the parts but there is no error now.

wait(1)
local DeleteEgg = game.Workspace.DeleteEggs


while true do
	wait(1)
	workspace.EggFolder.ChildAdded:Connect(function(egg)
		game.Workspace.EggFolder.EggKiller.Touched:Connect(function(partTouching)
			if partTouching.Name == "DeleteEgg" then
				game.Workspace.EggFolder.EggKiller:Destroy()
			end
		end)
	end)
end

Change “DeleteEgg” to “DeleteEggs”.

don’t do that it will lag the game.
make your script just this:

workspace.EggFolder.ChildAdded:Connect(function(Egg)
	Egg.Touched:Connect(function(partTouching)
		if partTouching.Name == "DeleteEggs" then --Replace DeleteEggs with whatever the part to delete eggs is actually named.
			Egg:Destroy()
		end
	end)
end)
2 Likes

This did work! *didnt notice that oops

Yet, once again this only worked on the first egg.

You should remove the while loop and replace “game.Workspace.EggFolder.EggKiller” with the argument “egg”.

2 Likes