How to make part delete after another one is spawned?

I have a script that spawns a part that you mention in the script.
But, I want a limit to how many of that part is cloned.

Basically, making it so that you can only spawn 3 parts, but if you spawn more than 3, it deletes one clone and replaces it with the new one.

local Event = game:GetService("ReplicatedStorage"):WaitForChild("ClickEvent")
local multiplier = 1

function click(_, position, particleName)
	local part = script[particleName]:Clone()
	local particles = part.CoreAttatchment:GetChildren()
	part.Size = Vector3.new(1,1,1)
	part.Position = Vector3.new(position.X, position.Y, position.Z)
	part.Parent = workspace
	for _,particle in pairs(particles) do
		local emitcount = particle:GetAttribute("EmitCount") or 1
		particle:Emit(multiplier * emitcount)
	end
end

Event.OnServerEvent:Connect(click)
local Clones = {}

if #Clones == 3 then
        -- Just make one
else
        Clones[1]:Destroy() -- Remove oldest clone
        Clones[1] = Clones[2] -- Move them down the table
        Clones[2] = Clones[3]
        Clones[3] = -- Make a new clone
end
2 Likes

Is this right?

Not quite for what your doing with it, as in for your script, if you are creating the particles you need to use that as the basis of making new particles as opposed to just cloning. That was just a example.

Wouldn’t it clone the entire thing? Or would that need a different script.

If you are cutting and pasting and hoping to get a working script out of it ill stop you there, :clone() just copys something already existing, you would need to use your function.

Well I’d assume after it clones the part, I could go off of that clone, and make it delete the first clone when the 4th appears

Oh my, id suggesting going back to basics, check out some videos and the apis before butchering other peoples scripts. Maybe look at tables and debounce if you want it to be relevant to that specific use