: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