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

Script of destruction? Is this possible to do?

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

4 comments

0

yeah

0

can you give me an example of how to do this

0

If you post something that shows you took a stab at it, yes!

0

Oh, sorry I answered.

2 answers

✓ Accepted answer

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

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

This discussion is preserved as it was. New answers, comments, and votes are closed.