Hello. I need a script that gets rid of the workspace in my game. Something that gets the children of a workspace, and then deletes all the children when the game is launched. Any scripts?
COMMUNITY QUESTION
Script that gets rid of the workspace?
3 answers
To iterate over all children of Workspace, destroying each, we'll use:
- The generic for statement
- The
ipairsfunction - The
workspaceglobal variable - The
Instance:GetChildrenmethod - The
Instance:Destroymethod - The
Instance:IsAmethod.
for _, child in ipairs(workspace:GetChildren()) do
if not child:IsA("Terrain") then
child:Destroy()
end
end
Note our use of Instance:IsA to check if the child is a Terrain, which cannot be destroyed and throws an error upon an attempt.
Normally, I'd recommend to use the :ClearAllChildren() method, but that won't work properly with Workspace as the Terrain Instance may not be deleted. So instead, simply loop through the children and delete them.
for _, v in pairs(workspace:GetChildren()) do
if not v:IsA("Terrain") then
v:Destroy()
end
end
2 comments
0
You'd have to use Instance:GetChildren to get the children
0
Fixed
Dunno why you'd want to do this but ok..
for _, child in ipairs(workspace:GetChildren()) do
if not child:IsA("Terrain") then
child:Destroy()
end
end
This discussion is preserved as it was. New answers, comments, and votes are closed.

4 comments
Pcall should not be used in this case, the only reason an exception would be thrown when attempting to destroy a part is if you're destroying the Terrain instance. It's better to just check the class type using the :IsA method.
I think destroying characters may also cause an error
Destroying characters does not throw an error.
Just checked, my bad.