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

How to destroy objects within a folder if it contains an instance?

I have been trying to destroy objects within a folder if it contained a part named "Fake".

Basically there's a folder than has 10 different parts. some parts have a part within them named "fake", how would I destroy the parts that contain the fake part inside them

script.Parent.MouseButton1Click:Connect(function()
	
	local fakepart = game.Workspace["Bridging the Gap"].Map["1"].Piece.Handle:FindFirstChild("Fake")
	
	
	if  game.Workspace["Bridging the Gap"].Map["1"].Piece.Handle:FindFirstChild("Fake") then 
		game.Workspace["Bridging the Gap"].Map["1"].Piece:Destroy()
	
	end

end)
	

1 answer

✓ Accepted answer

Use a for loop to go through each instance within the folder.

script.Parent.MouseButton1Click:Connect(function()
	for _, piece in pairs(game.Workspace["Bridging the Gap"].Map["1"]:GetChildren()) do
		if piece.Handle:FindFirstChild("Fake") then
			piece:Destroy()
		end
	end
end)

More on for loops here https://developer.roblox.com/en-us/articles/Loops https://www.youtube.com/watch?v=1ZAoVu9chN4

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