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!
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!
You can use :GetDescendants().
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.
Doing that is more effective than using :GetDescendants(). Youâre fine Iâm sure
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.
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
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
I do have my NPC inside a folder in workspace already, iâll try that
But since IsDescendantOf is returning a boolean value, how can I use it to determine what I even select?
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.
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
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