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

How can I get only certain parts inside my folder to destroy itself?

I am trying to create a script where if you touch a part in the Folder, it destroys itself. However, I also have things inside the folder I don't want to destroy, such as some remote events and integer values. I tried using this script but it doesn't seem to work.

for index, value in pairs(script.Parent:GetChildren()) do
	if value then
		value:Destroy()
	end
end

1 answer

✓ Accepted answer

What you should do to solve this problem is to use the :IsA method. This allows you to filter out certain things as a condition when used in an if statement.

Since it seems like you only want to destroy parts when a player touches it, you should do the following.

for index, value in pairs(script.Parent:GetChildren()) do
	if value:IsA("BasePart") then
		value.Touched:Connect(function(hit)
			local human = hit.Parent:FindFirstChild("Humanoid")
			if human then
				value:Destroy()
			end
		end)
	end
end

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