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.
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.
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.
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
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.
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
--// 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
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