Check if a build is touching the baseplate, or if another build is touching a build that is touching the baseplate?

Hey! So I am attempting to get a script to see if a part or build is touching a baseplate or touching another part that is touching the baseplate, and if it isn’t touching a part that is touching the baseplate, it would destroy it, how would I do this? Here is what I’ve been trying so far, it’s been printing how it should I think, but I am unsure how to continue;

local builds = game.Workspace.Builds
function recheck()
  for i,v in pairs(builds:GetChildren()) do
    if v then
      for r,c in pairs(v:GetTouchingParts()) do
        if c then
          if c.Name == "Baseplate" then
            return true
          else
            for x,t in pairs(c:GetTouchingParts()) do
              if t.Name == "Baseplate" then
                return true
              else
                return false
              end
            end
          end
        end
      end
    end
  end
end
while wait(.1) do
  local tr = recheck()
  if tr == true then
    print('true')
  else
    if tr == false then
      print('false')
    end
  end
end
4 Likes

If you just simply want to destroy “builds” just do:

builds:Destroy()

inside the if statement:

if tr == true tehn

you can also just do

if tr then
1 Like

The folder is a folder with all the builds in the map, that a person or other person(s) have placed, I am checking if the build was placed on another build that is touching the baseplate, if that build isn’t touching the baseplate, then is another build touching the build that is touching the build? If it isn’t, then it destroys all the builds it is touching, like a fortnite type of thing.

1 Like

You could use :ClearAllChildren() if you are wanting to delete the contents of the folder.

1 Like

Okay there is clearly confusion, I do not want to delete all the children in the folder, as some are touching the baseplate. In fortnite, when someone shoots a build ( or base of the build ) down, then it will destroy the rest that is touching it, this is what I am attempting to do, I am not attempting to clear the folder.

Actually now that I think about it, whenever a player places anything I would also set an object value to the adjacent build. Like walls on a floor for instance. Then before the floor gets deleted I would queue the walls for deletion as well. This should create a sort of web or tree that causes any build that has no dependency to be deleted.

Alright. Whenever you find a part that is touching the base, just add it to a table and then return the table in the function and loop through the table deleting the parts. If you need help implementing that into your code, let me know.

I am back, I do need help implementing that, would I insert the part touching the baseplate to the table, and then the parts that are touching that part touching the baseplate in the table, and the parts touching the parts touching the baseplates? It’s a big pattern, but is this how it would be done? And then it checks if those parts exist, and if they don’t it all gets deleted?

Here is an implementation to your code:

local builds = game.Workspace.Builds
function recheck()
	local list = {}
  for i,v in pairs(builds:GetChildren()) do
    if v then
      for r,c in pairs(v:GetTouchingParts()) do
        if c then
          if c.Name == "Baseplate" then
			table.insert(list,v)
          else
            for x,t in pairs(c:GetTouchingParts()) do
              if t.Name == "Baseplate" then
				table.insert(list,v)
              end
            end
          end
        end
      end
    end
  end
return list
end
while wait(.1) do
  local tr = recheck()
  if #tr > 0 then
    for _,v in pairs(tr) do
	v:Destroy()
end
  else
    if tr == false then
      print('false')
    end
  end
end

What I do is add all of the parts touching the baseplate to the table which is returned when you call recheck()

1 Like

Hey! Thanks for the help, it’s deleting every build I place though?

https://i.gyazo.com/99dc6686ec837b990dec3dd94cc6a089.mp4

All of the parts on the build are touching something that is touching the base plate then.

It’s keeping the builds that aren’t touching the baseplate?
https://i.gyazo.com/42f335d9982f6dce535c45b314faf36a.mp4

This is more what I was looking for;
https://i.gyazo.com/044ba948d9b038fb25a7ddbbf9985dce.mp4
This isn’t what I am receiving, and the part the builds are most likely touching is the player, and I want the build to stay if its touching the player, not all of the sudden delete, it is touching the baseplate.

This is what you described you wanted the script to do. Doing what Strucid did with when the base build is destroyed is a bit more difficult.

Wait no I meant the build destroys if it isn’t touching a part that is touching the baseplate, if the part is touching a part that is touching the baseplate it stays, if the part is also touching the baseplate it stays, I didn’t want it to destroy parts once placed on the baseplate? This would ruin the point of a building system

Just wondering, are the models anchored too?

Yes, they are actually mesh parts, they are anchored, and can collide is on.

Here’s a new script I made for you.

You’ll have to create an invisible hitbox for the part though which you add to the folder/group.

Here is the code:

local builds = game.Workspace.Builds

local function GetTouchingParts(part)
   local connection = part.Touched:Connect(function() end)
   local results = part:GetTouchingParts()
   connection:Disconnect()
   return results
end

local function check(folder)
	
	local list = {}
	
	for _,v in pairs(folder:GetChildren()) do
		
		local base = false
		
		local parts1 = GetTouchingParts(v)
		
		for _,a in pairs(parts1) do
			
			if a.Name == "Baseplate" then
				
				base = true
				
			else
				
				local parts2 = GetTouchingParts(a)
				
				for _,b in pairs(parts2) do
					
					if b.Name == "Baseplate" then
						
						base = true
						
					end
					
				end
				
			end
			
		end
		
		if not base then 
			
			table.insert(list,v)
			
		end
				
	end
	
	return list
		
end

while wait(.1) do
	
	local result = check(builds)
	
	if #result>0 then
		
		for _,part in pairs(result) do
			
			part:Destroy()
				
		end
		
	end
	
end

Here is the post from where I got the GetTouchingParts function from: (it only finds parts that are within the part, not touching the edge)

Does it have to be can collide on?

If you make it can collide off, you won’t be able to walk on the ramps though. I edited my message with a different solution.

So I make a part that surrounds that part?