How to get the models that touch a specific model?

How could I get a list of models which have a part touching a specific model?

P.S: GetTouchingParts() is for parts, not models. I’m not sure if it can be used in a way that solves my problem, but correct me if I’m wrong.

Thanks in advance!

Models dont touch models, it’s the parts who touch other parts. Models eventually are only collections of parts , the parts are the objects which touch/can be touched. You could of course use unions, meshes, etc.
But if you meant to get the parent of the touching parts, try use .Parent connected to the touching part(s).
You’d have to iterate through the model children and get the touching parts from that.

touchingPart.Parent will give you the model the part is in if it is in a model

local function modelIsTouched(model, thePart)
	for i,v:BasePart in pairs(model:GetDescendants()) do
		local TouchingParts = v:GetTouchingParts()
		for _, part in pairs(TouchingParts) do
			if part == TargetPart then
				return true
			end
		end
	end
end

local Model = path.to.model
local TargetPart = path.to.part

local partTouching = modelIsTouched(Model, TargetPart)

if partTouching then
	-- do something
end

If the model has a huge collection a parts, there is a possibility of lag, but this is how you could get a model’s touching part.

I have tried that. Many, many times before. It just turned into one gigantic mess, and it didn’t even show all the models that I knew it touched when I checked the output.

Try out what @NicoleSydor has suggested.

I tried it, but upon pasting it appears “TargetPart” is undefined.

Have you defined it? path.to.part means you need to get it , something like -
local it = game.workspace.part

you need to replace it with the part that you want to check
if you’re absolutely stuck and have tried everything on your own, put this in a part.

local touchPart = script.Parent

touchPart.Touched:Connect(function(touchingPart)
	for i,v in ipairs(touchPart:GetTouchingParts()) do
		local holdingModel = v:FindFirstAncestorWhichIsA("Model")
		if holdingModel and holdingModel:FindFirstChildOfClass("Humanoid") then
			print(holdingModel)
		end
	end
end)

I’m assuming you’re looking for character models, which is why it checks for humanoid

It’s in a function, so it should be defined when the function is called. Should I add an extra argument to the local function?

P.S. I am not looking for character models.

You could use methods of finding parent models, in order to find what models are touching. You can still USE GetTouchingParts(), or whatever method you are using to detect said parts. An example of what I am talking about below. Also, if a part is further down the line in the model, grabbing just the parent won’t do.

--Calling this on the part that has been touched, and we want to find what model it is in, if it is in one.
--You can add what you need onto this
local function FindModel(Part)
	local PossibleModel = Part:FindFirstAncestorWhichIsA("Model")
	if PossibleModel then
		return PossibleModel
	end
end

you can remove

and holdingModel:FindFirstChildOfClass("Humanoid")

if you don’t want it to only give you character models

I want to check all parts. The entire model. Do I do a for loop to check that?

it depends what you’re trying to do? you say you want the model but you want the model to get the touching parts which you use to get the model?

You’re being confusing now, could you explain what your trying to achieve more in depth?

Well actually you can (probably) because once I had a error in my script that somehow lets the baseplate and spawnlocation touch it, it was something like this.

script.Parent.Touched:Connect(hit)
     if hit then
            print(hit.Name)
      end
end

this isn’t the actual script

Okay. Here’s what I’m trying to do in detail:

Let’s say the pine tree is my model. I want to run a function that will return the other models that the pine tree is touching.

Although it seems like using .Parent on :GetTouchingParts() is my only option, I was asking if I needed a for loop to loop through the descendants in the pine tree and get their touching parts and from there, .Parent the returned output in order to get the touching models. Hope this clears up any confusion. That should return the two classic trees that the pine tree is touching.

You should be able to just make an invisible part in the pine tree that covers all of the leaves at once so you don’t have to loop through all of them, you’d need to make sure the touchedPart is not a child of the pine tree though.
if touchedPart.Parent~=script.Parent.Parent

That being said, if were to create an invisible part, and the classic tree was, let’s say, here:

That would still return the tree because the tree is within the invisible part.

You can make the invisible part way smaller, but if you are looking for accuracy, you can loop through each individual leaf or use several large parts that take up segments of the tree so it has less parts to search through