I have a script that spawns in a part along with particles (particles are disabled for now)
But I need to make it so that the parts get destroyed if more than three are already cloned.
Basically, if you spawn 3 parts, you can only have three, therefore deleting the first clone and spawning a new one.
local Event = game:GetService("ReplicatedStorage"):WaitForChild("ClickEvent")
local multiplier = 1
function click(_, position, particleName)
local part = script[particleName]:Clone()
part.Parent = workspace
local particles = part.CoreAttatchment:GetChildren()
part.Size = Vector3.new(1,1,1)
part.Position = Vector3.new(position.X, position.Y, position.Z)
for _,particle in pairs(particles) do
local emitcount = particle:GetAttribute("EmitCount") or 1
particle:Emit(multiplier * emitcount)
end
end
Event.OnServerEvent:Connect(click)
Assuming the parts are in workspace, you could probably do smthn similar to this:
function CLICK(param1,param2,param3)
local count = 0
for i, part in pairs(workspace:GetChildren()) do
if part:IsA(âPartâ) and part.Name == âYOUR_NAMEâ then
counter += 1
end
end
if count == 3 then
-- destroy a part
end
end
Dunno if this is exactly what youâre looking for so sorry if this doesnât fit your situation/scenario
function CLICK(param1,param2,param3)
local count = 0
for i, part in pairs(workspace:GetDescendants()) do
if part:IsA(âPartâ) and part.Name == âYOUR_PARTNAMEâ then
counter += 1
end
end
if count == 3 then
-- destroy a part
end
end
I changed workspace:GetChildren() to workspace:GetDescendants()
Oh, replace the â==â with â=â and see if it stops erroring.
Also, I assume that your partsâ names change depending on how many there are so Iâd probably add a boolvalue inside and add if part:FindFirstChild(âValueâ) then
local count = 0
for i, part in pairs(workspace:GetDescendants()) do
if part:IsA(âPartâ) and part.Name == âYOUR_PARTNAMEâ then
counter += 1
end
end
if count == 3 then
-- destroy a part
end
end
Okay, since that doesnât seem to work; maybe revert all the changes you just made and try @luketeam5âs method to see if their method works (which it seems like it will)
after that, you add the new part in your function to the table using table.insert
function click(_, position, particleName)
[...]
if parts[4] then -- check if something exists at the 4th position
parts[4]:Destroy() -- destroy the part
end
table.insert(parts, 1, part) -- insert the part to 1st position in the table
[...]
end
No, you need to call the table.insert after cloning the part, also Iâve made a mistake (edited) because I am on phone:
The table.insert should be like this
table.insert(parts, 1, part)
I also suggest moving part.Parent after part.Position as that helps with performance.