How to destroy part after click?

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)
6 Likes

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

Is it different if the part is elsewhere, because the script I posted is in ServerScriptService, connected to the script it’s in.
Screen Shot 2023-07-01 at 5.44.46 PM

You could prolly do this then:

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()


Just noted that there are lines indicating incorrect placement, haven’t changed anything.

Okay so I misspelled “count” at the bottom (I put counter += 1 when it’s supposed to be count += 1).

For the other issue, could you hover your mouse over the erroring lines and tell me what it says?

P.S. put the code I provided inside your existing function, remove the “function CLICK” part (I was using that to show where you should put it).

This is just for the “Part”

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

Doesn’t show any incorrect parts now, but the part doesn’t seem to be appearing anymore.

Is it staying in ServerScriptStorage? Or is it just plain out invisible / doesn’t exist.

Wait OHHHH put the code I gave you into your old function

Like I stated before, I just made a new function to show you where to put it

If you dunno what I mean:

function click() this is your ORIGINAL FUNCTION
put the code I gave you here (don’t copy and paste the function part)
end

(also when entering the game, the script in ServerScriptService just disappears)

Put this:


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

Into here:

IMG_2660

You could create a table and insert the Parts into it at the start using table.insert and always destroy table[4].

1 Like

I did this already, but it doesn’t change anything, the part still doesn’t spawn in.

(also I’m not sure if the parameters are correct, if I need 3 or just position)

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)

Haven’t ever tried making tables before…

So… uhhh I think they might provide an example and if not uhh well; I’m kinda on a time crunch sorry :sweat_smile:

THIS IS AN EXAMPLE

local tab = {}
table.insert(tab, part)

if #tab > 3 then -- greater than 3 parts
then destroy a part
end

You can declare a table like this:

local parts = {}

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

I also suggest learning more about tables:

1 Like

Is this positioned correctly?

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.