|
| 09 Feb 2014 04:15 PM |
Is there any way to search through the hierarchy of an object and return everything? How would I do that? I'm sure it isn't as simple as just :GetChildren() There has to be more to it than that. |
|
|
| Report Abuse |
|
|
wazap
|
  |
| Joined: 29 Jun 2007 |
| Total Posts: 23234 |
|
|
| 09 Feb 2014 04:16 PM |
I'm a bit confused as to what this question is asking for.... Wouldnt it be :GetFullName() and then work from there? |
|
|
| Report Abuse |
|
|
| |
|
|
| 09 Feb 2014 04:32 PM |
| wazap im talkin about returning an object's children and it's children's children and their children's children's children and so on and so forth and then printing them. |
|
|
| Report Abuse |
|
|
dodleman
|
  |
| Joined: 22 Dec 2007 |
| Total Posts: 1118 |
|
| |
|
smiley599
|
  |
| Joined: 23 Jan 2010 |
| Total Posts: 21869 |
|
|
| 09 Feb 2014 04:48 PM |
It would just a painfully long loop.
for i,v in pairs(workspace:GetChildren()) do for k,t in pairs(v:GetChildren()) do for q,p in pairs(t:GetChildren()) do --etc etc end end end |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2014 04:57 PM |
| i know there's a simpler way of doing it. galaxen showed me once but i don't have that message anymore and i havent seen him on in a long time. |
|
|
| Report Abuse |
|
|
Sehnsucht
|
  |
| Joined: 10 Apr 2011 |
| Total Posts: 990 |
|
|
| 09 Feb 2014 04:59 PM |
| The use of recursion would dynamically loop through your model. |
|
|
| Report Abuse |
|
|
Sehnsucht
|
  |
| Joined: 10 Apr 2011 |
| Total Posts: 990 |
|
|
| 09 Feb 2014 05:00 PM |
Here's an example from the Rbx.Wiki
function colorAllThePartsIn(parent) for _, child in ipairs(parent:getChildren()) do if child:IsA("BasePart") then child.BrickColor = BrickColor.new("Bright red") elseif child:IsA("Model") then colorAllThePartsIn(child) -- recursive step end end end colorAllThePartsIn(workspace) |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2014 05:02 PM |
function search(model) for i, v in next, model:GetChildren() do if not v:IsA("Model") then --stuff else seach(v) end end end |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2014 10:29 PM |
@Sehnsucht
It does indeed work but I'm having a great difficulty understanding how.
For 1, I do not know what _, is and I can't seem to find anything in the wiki that explains what it actually is.
2, function colorAllThePartsIn(parent) I don't see anything in the script that defines the parent, so I don't understand how that works. Even placing the script into a model colored everything outside the model as well.
Although I'm guessing that can be attributed to colorAllThePartsIn(workspace), where again I don't see anything in the script that defines where workspace is.
It's cool that it works but without being able to understand it, there's nothing I can do with it. Perhaps you can explain it in detail? (and please use simple words xD, there's a reason I don't like using the wiki...) |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2014 10:31 PM |
| Well I did figure out what workspace meant. I didn't realize it but you can type game.Workspace as just workspace. I didn't know that before. Changing it to script.Parent allowed me to localize the search into just the model the script was in. I still do not know what _, is, however. |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2014 10:34 PM |
I remember that one can use variables inside function names to help with the script. Kinda like script.Parent.Touched:connect(function(hit)
With that figured out the only thing left to understand is what _, is xD |
|
|
| Report Abuse |
|
|
Sehnsucht
|
  |
| Joined: 10 Apr 2011 |
| Total Posts: 990 |
|
|
| 09 Feb 2014 11:01 PM |
It's a for each loop.
for index,value in pairs(tab) do
end
You put any table for "tab", and it will iterate through the table. For every element "index" will be assigned the position and "value" will be the value at that index.
for example,
tab = {"ten", "Sehnsucht", 1, nil} for i,v in pairs(tab) do print(i, v) end --> 1 ten --> 2 Sehnsucht --> 3 1 --> 4 nil |
|
|
| Report Abuse |
|
|
|
| 09 Feb 2014 11:02 PM |
| ok so it's like a simplified command telling the code to "Search every nook and cranny" |
|
|
| Report Abuse |
|
|
Sehnsucht
|
  |
| Joined: 10 Apr 2011 |
| Total Posts: 990 |
|
|
| 09 Feb 2014 11:09 PM |
Basically yeah it just searches through everything.
It's up there with the most useful things I've used in 5 years of coding in lua. I use it practically in every script.
You can use :GetChildren() with nearly all models to return a table of everything inside it.
Workspace:GetChildren() --> will give you a table of all the things inside workspace that you could use to loop through. |
|
|
| Report Abuse |
|
|