Help with ignoring certain parts in a loop

Currently working on a random plugin for a challenge from a friend, but I cant figure out how to get the loop to ignore certain parts with certain names, like when it goes to delete stuff when it hits terrain(which it cant delete) it just gives and error and stops.

Any way to ignore certain names with the loop?

local toolbar = plugin:CreateToolbar("Why, why did you download this")
local newScriptButton = toolbar:CreateButton("oh no", "hehe", "")
local Opened = false
local Children = workspace:GetChildren()

newScriptButton.Click:Connect(function()
	while wait() do
		print("poggy")
		local widgetInfo = DockWidgetPluginGuiInfo.new(
			Enum.InitialDockState.Float,
			true,
			true,
			244,
			44,
			244,
			44
		)

		local testWidget = game.PluginGuiService:FindFirstChild("testWidget")
		local btn1 = nil

		if not testWidget then
			testWidget = plugin:CreateDockWidgetPluginGui("testWidget", widgetInfo)

			btn1 = Instance.new("TextButton")
			btn1.Name = "Button1"
			btn1.TextSize = 11
			btn1.AnchorPoint = Vector2.new(0.5,0.5)
			btn1.Size = UDim2.new(0.5,0.1,0.5,0.1)
			btn1.Position = UDim2.new(0.5,0.1,0.5,0.1)
			btn1.Text = "Dont press this"
			btn1.Parent = testWidget

			btn1.MouseButton1Click:Connect(function()
				while wait(2) do
					for i = 1,#Children do
						local CurrentChild = Children[i]
						CurrentChild:Destroy()
						if CurrentChild.Name == "Terrian" then
							CurrentChild.Parent = game.Lighting
							-- not sure what to do up to here
						end
					end
				end
			end)
		end
	end
end)
1 Like

This is a great situation to use table.find


source

Here’s an example of how you would use an array of strings along with table.find to ignore objects with certain names when iterating through the children of the workspace service

local namesToIgnore = { 'Terrain', 'SomeName', 'OtherName' }
local children = workspace:GetChildren()

for index, object in ipairs(children) do
   local isPartIgnored = table.find(namesToIgnore, object.Name) -- will eval to nil if part is not to be ignored

   if not isPartIgnored then
      print('Found:', object)
   end
end
2 Likes