How do I destroy all parts of a certain name when some have already been destroyed?

  1. What do you want to achieve? Keep it simple and clear!

I am making a round based game where the player collects plastic from the ocean, and I want to remove all plastic after the round ends.

  1. What is the issue? Include screenshots / videos if possible!

The issue is that some of the plastic has been collected, so it throws up an error when I try to destroy more plastic than there is. This then stops the script from running, which means that no more plastic is generated.

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I had an idea to check how much plastic is left, but I have no idea how do to do that.

If there is anything you would like me to supply then I will happily get it for you.

Thanks in advance!

2 Likes

How exactly are you destroying the plastic that it errors?. Can you supply some code so we can look at it.

Put all the plastic used in the game within the same folder or model. When the round is over just delete the folder or model used to store them.

2 Likes

This is the code:

local plastic = game.ServerStorage.Plastic
local parts = 60
local intermission = 30

while true do
    wait(intermission)
    for i = 1, parts do
		local pClone = plastic:Clone()
		pClone.Handle.BrickColor = BrickColor.Random()
		pClone.Parent = game.Workspace
		pClone.Handle.CFrame =     CFrame.new(math.random(-500,500),math.random(0,250),math.random(-500,500))
		wait(1)
	end
	for i = 1, parts do
		game.Workspace.Plastic:Destroy()
	end
end

This is in the script that spawns the plastic.

u can store ur plastic into a folder in workspace and checking how many plastic is there with:

print(table.getn(game.Workspace.Folder:GetChildren())
1 Like

Change parts from 60 to plastic:GetChildren()

EDIT: @nuttela_for1me’s solution is also effective.

1 Like

Going over the code you are just waiting 30 seconds making 60 “Plastics” and then making a infinite loop with the while true. I would suggest putting all the clones in a table and deleting them from there/Or using debris service for removing them after some time.

https://developer.roblox.com/en-us/api-reference/class/Debris

idk why are u destroying like crazy. ur destroying way faster than u are creating… remove destroying while loop and rather destroy a plastic when is touched by a player or player’s tool

Bad code practice, try something like this:

--// Services \\--
local ServerStorage = game:GetService("ServerStorage");
local Debris = game:GetService("Debris");

--// Variables \\--
local plastic = ServerStorage:WaitForChild("Plastic");

local intermission = 30;
local partsSpawning = 60;
local despawnRate = 20;

--// Round loop? \\--
while true do
    wait(intermission);
    for index, partsSpawning do
		local pClone = plastic:Clone();
		pClone.Handle.BrickColor = BrickColor.Random();
		pClone.Parent = game.Workspace;
		pClone.Handle.CFrame = CFrame.new(math.random(-500,500),math.random(0,250),math.random(-500,500))
		Debris:AddItem(pClone, despawnRate); --// Assuming you are wanting to destroy the plastic after a ammount of time
		wait(1)
	end
end

You can do something like

while true do
    wait(intermission)
    for i = 1, parts do
		local pClone = plastic:Clone()
		pClone.Handle.BrickColor = BrickColor.Random()
		pClone.Parent = game.Workspace
		pClone.Handle.CFrame =     CFrame.new(math.random(-500,500),math.random(0,250),math.random(-500,500))
		wait(1)
	end
	while workspace:FindFirstChild("Plastic") do --as long as workspace has something called plastic
		game.Workspace.Plastic:Destroy() --destroy
	end 
end

But in general, using while loops inside of while loop is a very bad practice. What you can do instead is, to make the game cleaner, create a folder under workspace that will contain the plastic, parent the plastic to that folder instead, then loop through that folder and destroy each child.

local plastic = game.ServerStorage.Plastic
local folder = workspace.Folder --create this
local parts = 60
local intermission = 30

while true do
    wait(intermission)
    for i = 1, parts do
		local pClone = plastic:Clone()
		pClone.Handle.BrickColor = BrickColor.Random()
		pClone.Parent = folder
		pClone.Handle.CFrame =     CFrame.new(math.random(-500,500),math.random(0,250),math.random(-500,500))
		wait(1)
	end
    for i, v in pairs(folder:GetChildren()) do
        v:Destroy()
    end
end

Sorry about the while true loop, that was a placeholder whole I looked for a solution. I have changed the code now.