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.
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
:GetTouchingParts()
is definitely the better option, so you should definitely do that. If the trees are name identifiable, then you can pick out the different models after looking up the ancestor tree as I posted. Using this, you can check every part touched to see what their model ancestor is. (Possibly use a table to see if that model has already been checked)
I will try that, thank you. Although, if there was a part within a folder within a model that was caught by GetTouchingParts
, and I used .Parent to get its parent model, It would just return the folder the part is in instead of its model. However I do have a solution to that. Thank you for your assistance.
Oh yeah, if you want an example of what im thinking here it is. Also I am not telling you to use .Parent, but get back to me if it works! (I have spent a lot of time on the efficiency of the below code, if this helps you make sure to mark it as a solution!)
local function FindModels(Part)
local ModelsFound = {}
local TPS = Part:GetTouchingParts()
for _, v in pairs(TPS) do
local PossibleModel = v:FindFirstAncestorWhichIsA("Model")
if PossibleModel and not table.find(ModelsFound, PossibleModel.Name) then
table.insert(ModelsFound, PossibleModel.Name)
end
end
--Proceed to do whatever is needed now that you know what models contained the touched parts
end
local Workspace = workspace --Local environment integration.
local Model = Workspace.Model --Example model.
local function OnTouched(TouchedPart)
local TouchedModel = TouchedPart:FindFirstAncestorOfClass("Model") --Get model of touching part.
if not TouchedModel then return end
print(TouchedModel.Name)
end
for _, Child in ipairs(Model:GetChildren()) do
if not (Child:IsA("BasePart")) then continue end --Ignore non-parts.
Child.Touched:Connect(OnTouched) --Touched connection.
end