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