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

Script of destruction? Is this possible to do?

Asked by 6 years ago
Edited 6 years ago

Is there any way I could destroy everything in the work space when the game is running?

0
yeah Azarth 3141 — 6y
0
can you give me an example of how to do this OptiBloxxer 9 — 6y
0
If you post something that shows you took a stab at it, yes! Azarth 3141 — 6y
0
Oh, sorry I answered. Bluemonkey132 194 — 6y

2 answers

Log in to vote
1
Answered by
Azarth 3141 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

You're going to want to learn about dictionary style arrays to prevent certain items from being deleted (think how people use admin scripts to stop you from entering a door), pcall() to prevent your script from stopping due to errors, ChildAdded for when objects are added during your game, and for loops to dump everything in the Workspace when your script loads.

local whitelist = {camera = true, terrain = true} -- names and classnames you don't want affected
local ws = game:GetService('Workspace')

local function remove_item(obj)
    -- if obj is not in our whitelist, then we proceed.
    if not whitelist[obj.ClassName:lower()] and not whitelist[obj.Name:lower()]  then
        -- Some items can't be removed, so use caution with a pcall() 
        if pcall(function()
            game.Debris:AddItem(obj, 0)
        end)then 
            print(obj.Name.. " was removed.")
        else
            print(obj.Name.. " couldn't be removed.")
        end
    else
        print(obj.Name.. " was on the whitelist")
    end
end

-- Anytime something is added to Workspace, it will get sent to the remove_item function.
workspace.ChildAdded:Connect(function(obj)
    remove_item(obj)
end)

-- Makes sure everything is removed from Workspace on load.
for i,v in pairs(ws:GetChildren()) do 
    remove_item(v)
end
Ad
Log in to vote
0
Answered by 6 years ago

How about this:

for i,v in pairs(workspace:GetChildren()) do
    if not v:IsA("Terrain") and not v:IsA("Camera") then
        v:Destroy()
    end
end

Answer this question