Still having errors deleting 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

but there is still some left

I don’t know what your goal is can you explain a little more

It is suppose to be looping threw the parts and deleting them slowly, 2 by 2, but it errors after awhile when their still are more parts.

Well, the error states that #Parts is empty, which essentially means 0.

How are these parts being created? If the client creates these parts, but the server tries to gather them. It will appear empty to the server, as the client cannot directly alter the server’s state.

1 Like

They are made in the map, stored in replicatedstorage which the map is cloned at the start of the game

If the map is cloned by the client, it’s not shared with the server. The cloned instance will only be visible to the client that cloned it.

If you want the whole server to see the same instance, you’d need to have the map instance on the server. Then, you could alter the map with a ServerScript.

Alternatively, you can create a system to pass data to the clients, instructing the client machines on which parts to modify on the map. The parts to modify would be decided by the server. However, this is a more exploitable route.

I recommend keeping the map on the server (Workspace), and altering it with a ServerScript. Don’t have the client clone the map.

It is cloning the map from the server, and placing it in workspace, inside of the map model I have the deleting script, shown here. https://i.imgur.com/ufvuct4.png

After around half of the parts are gone it errors.

Print the table before the error to see how the server sees the table before the second random or first random is ran

I think he meant the values inside the table.

I figured it out, thanks anyways