generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripting Helpers
Home Search
 

Re: Dynamically searching the hierarchy of an object?

Previous Thread :: Next Thread 
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
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 is not online. 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
DarkCreepers is not online. DarkCreepers
Joined: 29 Mar 2013
Total Posts: 915
09 Feb 2014 04:17 PM
[ Content Deleted ]
Report Abuse
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
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 is not online. dodleman
Joined: 22 Dec 2007
Total Posts: 1118
09 Feb 2014 04:47 PM
We call that recursing
Report Abuse
smiley599 is not online. 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
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
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 is not online. 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 is not online. 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
flatline115 is not online. flatline115
Joined: 29 Jul 2013
Total Posts: 7826
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
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
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
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
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
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
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 is not online. 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
xiaoxiao181 is not online. xiaoxiao181
Joined: 14 Aug 2008
Total Posts: 5882
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 is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripting Helpers
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image