Help in deleting models with a script

Hello, I’m having trouble in making a script that can delete specific types of models e.g
I want the script to delete a model named “Trees” inside that model is more models labeled “Tree”
How can I make it so that the script deletes the entirety of “Trees” and keeps the rest of the map

4 Likes

game.Workspace.Trees:Destroy()

you can get the child’s of the model named “trees” and then one by one with a for loop parent every child of trees to workspace and then in the delete the model trees

This is not what the OP is asking for

Loop through the model and make an if to check the model’s name. If it is the desired model, then destroy it
Edit:
Code example :

for _,x in pairs(game.Workspace.Trees:GetChildren()) do
  if x.Name == "Tree" then x:Destroy() 
  end 
end
3 Likes
local trees = workspace:FindFirstChild("Trees") -- find the model

local treeChild = trees:GetChildren() -- get childs

for I,v in pairs(treeChild) do -- for loop through the model
	if v.Name == "Tree" then -- if you find a model named tree
		v:Destroy()  -- delete trees
	end
end

Thanks to @Iukxs for pointing the error out as I am using mobile and wrote the script incorrectly.

2 Likes

Do what @TheWorstOne_2 said.

Just call the :Destroy method on the Trees model. When you delete a model all of its children get deleted too. This is on the intuition that all and only “Tree” models are in the “Trees” model

There is no point in iterating over the trees children and destroying the trees. It just creates more overhead function calls that are realistically not needed due to how ROBLOX’s :Destroy() method works.

1 Like

This isn’t what the OP asked for look at it again.

He/she wanted to PARENT all the child’s which are named tree to the workplace. Deleting the ancestor of a model will delete all the descendants and thus it wont be possible to change the parent of the child

OP said nothing of “parenting” children to workspace

Your intuition is good, but your reasoning is wrong.

You’re wrong but you are correct the op said nothing about parenting but if you understand what the OP was trying to say you might get it.

If you delete the whole tree model the parts which are not named “Tree” will also get deleted.
For example if the part is named “Rock” so if you delete
the whole model, rock will be deleted as well but the op doesnt want that instead want the rest of the map.

1 Like

You missed my post.

Doesn’t make sense to store rocks in a trees model. Anyways, this argument should stop now (or continue in private messages). We are at this point spamming on the post!

2 Likes

You’ve also got Model:ClearAllChildren() if you wish to clear the entire contents of a model.

According to @FastAsFlash_Dev that possibility has been marked out.

Which in my opinion is rather questionable, considering the information OP posted. (And why would you even put rocks in a trees model??)

No I mean like that was an example there can be many things instead of rocks.
IntValues, scripts, other models

OP didn’t state there was though so it is safe to assume that there isn’t.

Anyways I’m done responding. The post has been answered

1 Like

The script is written incorrectly - I have fixed it for you. The person also wanted to delete the trees, not set the parent to workspace, in that case you would need to swap v.Parent = workspace with v:Destroy(). The fixed script would look like this:

local trees = workspace:FindFirstChild("Trees") -- find the model

local treeChild = trees:GetChildren() -- get childs

for I,v in pairs(treeChild) do -- for loop through the model
	if v.Name == "Tree" then -- if you find a model named tree
		v:Destroy()  -- delete trees
	end
end

Yes, I am not sure if the op wanted to parent or destroy the child’s. But seems like op accepted my answer so I am assuming op wanted to parent, but @EpicMetatableMoment also said some valid points so I dont really know and I dont want to respond again since this post has already been bumped 10 times

That’s fine but the script you posted had many errors, and wouldn’t work.

Edit: Anyways, I’m done responding.

Local cannot be capitalized, also

if v.Name == "Tree" **do** -- if you find a model named tree
		v:Destroy()  -- delete trees
	end
end

wouldn’t work, and missing one end but it wouldn’t really matter since there is do instead of then.

1 Like

and if i want to remove a model but when its humanoid is = 0?
@FastAsFlash_Dev