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

Is There a way of destroying everything in the workspace in the same name?

Hello, is there a way i can delete everything that is called the same name in a script?

for example, i have everything as decal, i don't want to delete it manually, Is there a way?

local asdnew = Instance.new("TextButton",ScreenGui)
asdnew.BackgroundColor3 = Color3.new(0,0,0)
asdnew.BorderColor3 = Color3.new(0,0,0)
asdnew.Name = "nooties"
asdnew.Position = UDim2.new(1,-150,1,-90)
asdnew.Size = UDim2.new(0,150,0,45)
asdnew.Font = "SourceSansBold"
asdnew.FontSize = "Size14"
asdnew.Text = "Destroy"
asdnew.TextColor3 = Color3.new(255,255,255)
asdnew.MouseButton1Click:connect(function()
	game.Workspace.Sound:Destroy()
	game.Lighting:Destroy()
	game.Workspace.Terrain:Destroy()()
	game.Workspace.Camera:ClearAllChildren()()
	game.Workspace._CPData:ClearAllChildren()

2 answers

✓ Accepted answer

Hello, TheEdyGamerTroll23!

This script will destroy everithing with the names on table that are on workspace!

local DestroyList = {"ThingOne","ThingTwo"} -- Things name to destroy here(!!Just From Workspace!!)
local DestroyDescendants = {"ThingOne","ThingTwo"} -- Things name to destroy here(!!!From Workspace and its Descendants!!!)

function Destroy()
	for _,Object in pairs(game.Workspace:GetDescendants()) do
		for _,WorkspaceItem in pairs(DestroyList) do
			for _,Descendants in pairs(DestroyDescendants) do
				if Object.Name == WorkspaceItem then
					Object:Destroy() -- Destroys Workspace items
				end
				if Object.Name == Descendants then
					Object:Destroy() -- Destroys Childs of Items =D
				end
			end
		end
	end
end

Destroy() -- Calls the function to destroy the parts in workspace!

Read the comments!

This script will destroy when you PLAY the game, parts will still be in studio

Good Luck with your games!

11 comments

0

I mean that when i press a button it do the action i want

0

you can place this script on a function an then call it =D

0

so i can use the same script with a button, likfe Mouse1Click?

0

yes

0

This script can be on any part, it will aways get the workspace

0

Ok, Thanks! :D

0

I would be lots happy if you accept my answer =D

0

In Function Destroy() it tells me to change it to local :(

0

have you changed something inside of the function?

1

another advice: try not to name your functions the same name as roblox api functions such as Destroy()

0

Thanks for the advices, Nonaz!

local itemName = "ENTER ITEMNAME" --enter name of item here
local item = game.Workspace:FindFirstChild(itemName,true)
while item do 
  item:Destroy() 
  item = game.Workspace:FindFirstChild(itemName,true)
end

it's slow but it works :D

or loop over everything

local function destroyAll(list)
  for index, child in pairs(list) do
    if child.Name == "ENTER NAME HERE" and child:IsA("Decal") then --enter item info here
       child:Destroy()
    else
       destroyAll(child:GetChildren())
    end
  end
end
destroyAll(game.Workspace:GetChildren())

allows you to check the item type and is much faster if you have lots of stuff.. ALL CODE UNTESTED bugfix for yourself

1 comment

0

so i did test it and it works afterall yay no typos

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