Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Script that gets rid of the workspace?

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?

3 answers

To iterate over all children of Workspace, destroying each, we'll use:

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.

4 comments

0

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.

0

I think destroying characters may also cause an error

0

Destroying characters does not throw an error.

0

Just checked, my bad.

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.