Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Script that gets rid of the workspace? [closed]

Asked by 6 years ago

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?

Closed as Not Constructive by User#24403, BenSBk, green271, and zblox164

This question has been closed because it is not constructive to others or the asker. Most commonly, questions that are requests with no attempt from the asker to solve their problem will fall into this category.

Why was this question closed?

3 answers

Log in to vote
1
Answered by
BenSBk 781 Moderation Voter
6 years ago
Edited 6 years ago

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.

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. Customality 21 — 6y
0
I think destroying characters may also cause an error BenSBk 781 — 6y
0
Destroying characters does not throw an error. Customality 21 — 6y
0
Just checked, my bad. BenSBk 781 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

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
0
You'd have to use `Instance:GetChildren` to get the children BenSBk 781 — 6y
0
Fixed Customality 21 — 6y
Log in to vote
0
Answered by
s_21 74
6 years ago

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