How do I delete spawner when touching parts?

I need help with my zombie spawn. I am trying to get it where 3 parts are touched and it destroys the zombie spawner. This is what I have done

I made a Folder (called Spawns), dropped a part in the folder and here is the script. Here is my zombie spawner

    local spawns = script.Parent
    local spawn_time = 10



while true do
	wait(spawn_time)
	for _, spwn in pairs(spawns:GetChildren()) do
		if spwn:isA('BasePart') then
			local zombieCopy = game.ReplicatedStorage['Zombie']:Clone()
			zombieCopy.Parent = game.Workspace
			zombieCopy.HumanoidRootPart.CFrame = CFrame.new(spwn.Position + Vector3.new(0,3,0))
		end
	end
end

How can I make it to where if I touch 3 parts it will destroy the spawner and no more zombies will spawn?

I was thinking of something like this but it didn’t work. The zombies spawn in but when I touch the parts it will not destroy it so that no more zombies will pop out.

local spawns = script.Parent
local spawn_time = 10
local part1 = workspace.Part1

local part2 = workspace.Part2

local part3 = workspace.Part3

local touchedPart1 = false

local touchedPart2 = false

local touchedPart3 = false 


part1.Touched:Connect(function(hit)
	if hit.Parent == character then 
		partTouched1 = true
	end

	if partTouched2 and partTouched3 then
		spawns:Destroy()
	end
    end)
	part2.Touched:Connect(function(hit)
		if hit.Parent == character then 
			partTouched2 = true
		end

		if partTouched1 and partTouched3 then
		spawns:Destroy()
		end
end)

		part3.Touched:Connect(function(hit)
			if hit.Parent == character then 
				partTouched3 = true
			end

			if partTouched1 and partTouched2 then
		spawns:Destroy()
			end
end)
			while true do
				wait(spawn_time)
				for _, spwn in pairs(spawns:GetChildren()) do
					if spwn:isA('BasePart') then
						local zombieCopy = game.ReplicatedStorage['Zombie']:Clone()
						zombieCopy.Parent = game.Workspace
						zombieCopy.HumanoidRootPart.CFrame = CFrame.new(spwn.Position + Vector3.new(0,3,0))
					end
				end
			end

Well, here’s what I’d do

Instead of using bool values, you could just simply include a number value, & replace your while true do loop with a while Number ~= Goal do loop, cause the loop you’re using now is pretty much infinite

Although you’ll still need to include debounces whenever the part first gets touched, and make sure to Anchor them:

local spawns = script.Parent
local spawn_time = 10

local part1 = workspace.Part1
local part2 = workspace.Part2
local part3 = workspace.Part3

local Part1Enabled = false
local Part2Enabled = false
local Part3Enabled = false

local PartsTouched = 0

part1.Touched:Connect(function(Hit)
    if Part1Enabled == false then
        Part1Enabled = true
        PartsTouched += 1
    end
end)

part2.Touched:Connect(function(Hit)
    if Part2Enabled == false then
        Part2Enabled = true
        PartsTouched += 1
    end
end)

part3.Touched:Connect(function(Hit)
    if Part3Enabled == false then
        Part3Enabled = true
        PartsTouched += 1
    end
end)

while PartsTouched ~= 3 do
    wait(spawn_time)
    for _, spwn in pairs(spawns:GetChildren()) do
        if spwn:IsA("BasePart") then
		    local zombieCopy = game.ReplicatedStorage['Zombie']:Clone()
			zombieCopy.Parent = game.Workspace
			zombieCopy.HumanoidRootPart.CFrame = CFrame.new(spwn.Position + Vector3.new(0,3,0))
        end
    end
end

spawns:Destroy()

No zombies come out when I touch the parts but is there a way to make the spawn part physically get deleted from the game?

Check out this topic: My zombie spawning function wont work Hope this helps.

1 Like

You could literally just :Destroy() it the moment someone touches it inside the conditional checks

I thought the last line in the code would do that
spawns:Destroy()

It did stop the zombies from spawning, but I needed it to also destroy the spawner.

No, wait it worked. I was missing

spawns:Destroy()

at the end of the code. D’oh!!

I did add this:

spawns:Destroy()
local explosion = Instance.new('Explosion')
explosion.Parent = game.Workspace

as I also wanted the part to explode but when I touch the 3 parts the explosion happens at the player spawn point. It will also delay the 5 seconds to destroy itself. I wanted the explosion to happen at the zombie spawn part and I also wanted it to happen right when the 3 parts are touched, not the 5 seconds delay. Any help?