Help in script with ClearAllChildren()

Im making a script that deletes all childrens in workspace.

I want to delete all to see if the game runs better with nothing, but i dont want to remove all myself, so im making this script.

But i don’t want to remove the Terrain option, and when deletes all the script make an error bc cant remove this, so how i make the script removes all without the Terrain?

My script in ServerScriptServices

workspace:ClearAllChildren()

Well, you can filter out the terrain instance by using a for loop:

for _, instances in pairs(workspace:GetChildren()) do
    if (not instances:IsA("Terrain")) then
       --destory the other instances
    end
end

--or

for _, instances in pairs(workspace:GetChildren()) do
    if (not instances.Name == "Terrain") then
       --destroy the other instances
    end
end

P.S-
Don’t use :ClearAllChildren() with this

If you don’t understand for loops as yet, well, here’s the documentation on it:

https://www.lua.org/pil/4.3.4.html

1 Like

Yea, but my game creates parts and models with random names, so idk whats the names of parts/models (using math.random)
This is the reason i used the ClearAllChildren()
And tysm about the link :smiley: the link helpen me in “for” loops :smiley:

But, this removes all instances from the workspace except for the terrain instance when you use instances:Destroy().

2 Likes
for _, instances in pairs(workspace:GetChildren()) do
    if (not instances:IsA("Terrain")) then
       --destory the other instances
    end
end

instance:IsA() checks what type of object something is. So if the object is not a Terrain then we destroy the instance. This means if there are 2 objects named Terrain in workspace (One being the real one and the other being some random part) it will remove the part as it’s not a Terrain.

Basically instance:IsA() does not depend on the name of an object only what it is.

3 Likes

Yea but now how i destroy all inside without the Terrain? bc i need to destroy all inside workspace and a script is creating models and parts with random names.

They have provided solid solutions to the issue you are trying to fix. Try their code to see if it functions how you want it to.

1 Like

You can accomplish this with a generic/numeric loop and check if the object is the terrain.

local WorkspaceService = game:GetService('Workspace')
local GetChildren = WorkspaceService:GetChildren()

for i = 1, #GetChildren do
	local Target = GetChildren[i]
	if Target:IsA('Terrain') == false then
		Target:Destroy()
	end
end
1 Like

this works in normal script in ServerScriptService?

Bc its saying (script path “attempt to call a table value”)

Yes, this works in a normal script (server-script).
Store your scripts in ServerScriptService.

What are you removing exactly, BaseParts?

1 Like

Parts/Models/Mesh, so all blocks that are visible

Replace for i = 1, #GetChildren() with for i = 1, #GetChildren
My mistake.

2 Likes

worked, ty
U can change the last script to solve the mistake? (for solution)

1 Like

Just did that. And you’re welcome.

1 Like