Can I get the contents of all the models inside of a model?

If I have a model, and it has parts (and more models with parts) inside of it, how could I get all of these descendants?

Short question, sorry!

3 Likes

You can use :GetDescendants().

3 Likes

What I am trying to make is a GUI that follows my mouse (I know how to do that), but since I check for an instance inside of the object I hover on, right now it just checks for hover.Parent["instance"] and not hover.Parent.Parent["instance"] etc., I could check if the parents exist, and keep checking through them but idk if it will be effective.

1 Like

Doing that is more effective than using :GetDescendants(). You’re fine I’m sure

2 Likes

I have this, but it doesn’t seem to work very well…

local function checkParents(t)
	local a
	if t.Parent ~= nil then 
		a = t.Parent
	end
	if not a then
		a = t.Parent.Parent
	end
	if not a then
		a = t.Parent.Parent.Parent
	end
	if a:FindFirstChild("HumanoidRootPart") then
		return true
	end
end

^ I just realized this is written stupidly and makes no sense xD, trying to realize how to sort this out

This script is over complicating things. Try and name your variables something meaningful as well, it’s a good coding practice and will help you if you need to debug longer scripts.

2 Likes

You can also use :IsDescendantOf(). This function checks if the part is a descendant of another part. So in your case you could probably do:

local a = t:IsDescendantOf(parent)

This will return true, if “t” is descendant of “parent”. And will return false otherwise

2 Likes

This doesn’t really solve it, because how can I get parent properly?
I must still check it until I get to the ‘character’


This shows that what I hover over, is a model inside of that character.

So the ‘character’ is the parent you want to find? What is the character? A player character?
It looks like the character is an NPC, if im understanding this right.
I think you would be able to use “workspace.NPCs” instead of “parent” in above example

3 Likes

I do have my NPC inside a folder in workspace already, i’ll try that

2 Likes

But since IsDescendantOf is returning a boolean value, how can I use it to determine what I even select?

1 Like

It just checks if one part is descendant of the other part. You already know both parts when matching them in IsDescendantOf. In above example i use “t” and “a” as the parts. I hope this makes sense

I am back to point 1, where I needed to just get the actual model of the NPC…
Sure I can check if it belongs to NPCs and stuff but getting the model itself within all of the parts and models inside it…

local function checkParents(t)
	local a = t:IsDescendantOf(workspace.NPCs)
	if a then
		return true
	end
end
					if checkParents(mouse.Target) then
						print(mouse.Target.Name)
					end

Ah okay. Then instead of matching the part with the NPC folder, i would match it with every NPC in a for loop. And then return the NPC that it matches. Like this:

local function checkParents(t)
    for v, i in ipairs(workspace.NPCs:GetChildren()) do
        if t:IsDescendantOf(i) then
		    return i
	    end
    end
end

This would return the model if its found, or nil if not.

2 Likes

Wouldn’t it lag much if there were many NPCs to go through, or not because it only checks for 1 out of all?

Other than this question, that’s seems to be my best solution as well :smile:

1 Like

I don’t think it will lag. Unless you of have thousands of NPCs. You can check the scripts performance while playing, by pressing F9 and clicking on the “Scripts” tab :slight_smile:

1 Like