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

When in the area more than one object, delete one?

Hi, I need help with a script. When in the area is more than one object, need to delete one, but I either delete everything or no one.

I also wanted to make both the area and the part anchored, but in this case script does not work.

Area - anchored part, Part - select all parts in folder.

Please, help!

local Part = workspace:WaitForChild("Part"):GetChildren()
local Area = game.Workspace.Area

Area.Touched:Connect(function(hit)
	if hit.Name == "Part" then
		Part:Destroy()
	end
end) 

1 answer

✓ Accepted answer

Hello, if I understand well, you want to delete all parts in a certain area, except for one? Please tell me if I misunderstood.

local part = script.Parent -- area is the parent of this script


for i, v in pairs(part:GetTouchingParts()) do -- i is the variable keeping track of the index, v in the part itself, part:GetTouchingParts() returns a table of all parts touching the area
	if i ~= 1 then -- if index is not 1, delete the part, which means that the part with index 1 will not be deleted
		v:Destroy()
	end
end

This script should work if that's what you want.

Please elaborate on the anchored part.

1 comment

0

You got it right, thank you.

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