Ok, so I have a part inside a model inside a model inside workspace. I want to know what’s the model in workspace, but I dont know how many parents there are from the part to the model.
I didn’t try anything, because I really don’t know how to do it.
Thanks
If I understand you correctly, you’d have to loop through all the descendants of your model by doing this
for i, v in pairs(model:GetDescendants()) do
print(v.Name)
end
Well, I have the part and I have to find the “origin”
Oh well in that case you could continiously keep using :FindFIrstAncestorWhichIsA(“Model”) until it starts returning nil
A good way to do this is using what you call Recursion. Basically it’s calling the function in itself.
function GetHighestParent(part)
if part.Parent == workspace then
return part
end
return GetHighestParent(part.Parent)
end
Even though I named the parameter “part”, you can pass in any instance.
Sorry for the bad formatting—mobile is a little odd.
I used this code
local model = script.Value.Value:FindFirstAncestorWhichIsA("Model")
repeat wait()
if model:FindFirstAncestorWhichIsA("Model") ~= nil and model:FindFirstAncestorWhichIsA("Model").Name ~= "Workspace" then
model = model:FindFirstAncestorWhichIsA("Model")
end
until model.Parent.Name == "Workspace"
print(model.Name)
Use what you prefer, but both ways work.
But using the function above could clean up the code in my opinion. All you need to do to get the highest parent is:
local ModelInWorkspace = GetHighestParent(childOfModel)