Script abruptly stopping

Hello!

So I made a script that chooses a random certain part in a model and deletes it. It works fine, but somehow, the script always stops when there’s few parts left. I found the problem was on the if statement, but I don’t know how to fix it, and I need it to check if the certain part is right. All help is appreciated!

local Model = workspace.Grassland

for count = 1, #Model:GetChildren() do 
	local ChildrenOfModel = Model:GetChildren()
	local PartToDelete = ChildrenOfModel[math.random(#ChildrenOfModel)]
	if PartToDelete.Name == "LandPart" then --Where the problem is
		PartToDelete.Material = Enum.Material.Neon
		PartToDelete.BrickColor = BrickColor.new("Really red")
		wait(.1)
		PartToDelete:Destroy()
	end

	if #Model:GetChildren() == 0 then
		Model:Destroy()
		break
	end
end

I mean it won’t destroy anything else if its looking for only “LandPart” is there any others or are there multiple LandParts?

No, no other parts are named "LandPart;s, if there is, it would’ve been deleted by the script. Yes there’s multiple LandPart’s

It because you are using a Math.Random for the PartToDelete variable, Basically what’s going on is the script loops thru the models children but the Math.Random leaves missing parts because Math.Random can pick a same part it’s already landed on.

local Model = workspace.Grassland
for count = 1, #Model:GetChildren() do 
	local ChildrenOfModel = Model:GetChildren()
	local PartToDelete = ChildrenOfModel[count] --Where the problem was
	if PartToDelete.Name == "LandPart" then
		PartToDelete.Material = Enum.Material.Neon
		PartToDelete.BrickColor = BrickColor.new("Really red")
		wait(.1)
		PartToDelete:Destroy()
	end

	if #Model:GetChildren() == 0 then
		Model:Destroy()
		break
	end
end

I tested it, and it works perfectly fine.

I want the deletion to be random, though.

I also removed the LandPart statement, and it deleted all the parts, that’s how I thought that was the problem.

One Question the script should work as long as there are only “LandPart” named part’s

Yes, theyre only named landparts, all the parts I intended to delete.

local Model = workspace.Grassland
local PartToDelete
for count = 1, #Model:GetChildren() do 
	local ChildrenOfModel = Model:GetChildren()
	PartToDelete = ChildrenOfModel[math.random(#ChildrenOfModel)]
	
	if PartToDelete.Name == "LandPart" then 
		PartToDelete.Material = Enum.Material.Neon
		PartToDelete.BrickColor = BrickColor.new("Really red")
		wait(.1)
		PartToDelete:Destroy()
	else
		repeat 
			PartToDelete = ChildrenOfModel[math.random(#ChildrenOfModel)] 
			if PartToDelete.Name == "LandPart" then --Where the problem is
				PartToDelete.Material = Enum.Material.Neon
				PartToDelete.BrickColor = BrickColor.new("Really red")
				wait(.1)
				PartToDelete:Destroy()
			end
			wait()
		until PartToDelete.Name == "LandPart"
	end

	if #Model:GetChildren() == 0 then
		Model:Destroy()
		break
	end
end

See your script should work if all the parts in the model at named “LandPart” but just incase there aren’t this script should fully work

1 Like

There’s also other parts not named LandPart.

Okay, that explains your bug, so what I did was made it if the PartToDelete was not a LandPart then it repeats until it is a LandPart

1 Like

Ohh, but thanks for the solution!