Error removing parts after awhile

Near the last parts of the round, I get this error and it stops working

Error:

Basically, it should be removing the parts and play a little animation

Here is my code:



local PartsFolder = script.Parent.DisapearingParts

local Parts = PartsFolder:GetChildren()

while wait(2) do
	
	local index = math.random(1, #Parts)
	local PickedPart = Parts[index]
	
	local index2 = math.random(1, #Parts)
	local PickedPart2 = Parts[index2]

	table.remove(Parts, index)
	PickedPart.Color = Color3.fromRGB(255, 0, 0)
	PickedPart.Material = "Neon"
	wait()
	for i = 0, 1.05, 0.05 do
		PickedPart.Transparency = PickedPart.Transparency + 0.05
		wait()
	end
	
	PickedPart.CanCollide = false
	
	table.remove(Parts, index2)
	PickedPart2.Color = Color3.fromRGB(255, 0, 0)
	PickedPart2.Material = "Neon"
	wait()
	for i = 0, 1.05, 0.05 do
		PickedPart2.Transparency = PickedPart2.Transparency + 0.05
		wait()
	end

	PickedPart2.CanCollide = false

	
end

Edit: but there is still some left

Is there anything inside the DisappearingParts folder?

Yes, there should be due to their are still parts left.

Here.

Eventually, all the parts disappear, meaning #Parts evaluates to 0. Causing the interval to be from 1 to 0. Try checking if there are none left before executing

e.g.

while wait(2) do
.....
    if #Parts < 1 then
    -- Do stuff that should happen when the game ends   
    break
    end   
.....
1 Like

According to your error there is nothing inside the folder, meaning the interval for math.random is empty.

But there are more left on the map?

Exactly, when there are no more left, it bugs.

You need to make sure that:
If there are no more parts, stop deleting them (the round ends)

1 Like

There still are more?