|
| 25 Jul 2012 07:01 PM |
I was kind of bored, so I decided to make a FindFirstChild-type function, except for parents rather than children. Since it will probably be useful to just about every scripter at one time or another, I decided to share it with you all. ;)
The name would probably make you expect it to see if the parent of an object's name is a certain string. However, that is not the case, as that would be useless. It will keep searching the object's ancestors until it finds an object with the specified name.
function FindFirstParent(obj, name) --Declares function as well as two parameters local result = obj --Declares local variable named 'result' repeat --Initiates repeat loop result = result.Parent --Sets result's value to its Parent if not result then --If the new result does not exist return nil --Return nil (duh) end --End if statement until result.Name == name --End repeat loop when a match is found return result --Return result (again, duh) end --End function
It is used the same way as FindFirstChild, except since it's a function, not a method, you don't run it ON the object. Here is an example:
local m = Instance.new("Model", workspace) local p = Instance.new("Part", m) local mesh = Instance.new("SpecialMesh", p) p.Anchored = true local model = FindFirstParent(mesh, "Model") if model then Instance.new("Message", workspace).Text = model.Name .. " has been found." end
Enjoy. ;) |
|
|
| Report Abuse |
|
|
|
| 25 Jul 2012 07:20 PM |
| Bump. Not even people have seen this. |
|
|
| Report Abuse |
|
|
| |
|
| |
|
| |
|
| |
|
|
| 25 Jul 2012 08:06 PM |
| ...Wow, thanks for the reply. :C |
|
|
| Report Abuse |
|
|
|
| 25 Jul 2012 08:11 PM |
This too hard for da forum. :o
|
|
|
| Report Abuse |
|
|
|
| 25 Jul 2012 08:13 PM |
| @pokemon - Too hard? I think not. It's just a function, a variable, and a repeat loop. Besides, this isn't meant to teach people, it's to help them. |
|
|
| Report Abuse |
|
|
| |
|
| |
|
|
| 25 Jul 2012 09:06 PM |
This is to complicated for me. I'm gonna go look for some ice cream.
The scripty stuff looks cool though :| |
|
|
| Report Abuse |
|
|
Strieken
|
  |
| Joined: 17 Jun 2011 |
| Total Posts: 3058 |
|
|
| 25 Jul 2012 09:07 PM |
unless you had an object that was buried deeeeeeep inside a model filled with tons of other objects and stuff, this wouldn't be all that useful.
i do think its a good idea though.
btw
hi |
|
|
| Report Abuse |
|
|
|
| 25 Jul 2012 09:08 PM |
| An object can only have one parent, derp. |
|
|
| Report Abuse |
|
|
Strieken
|
  |
| Joined: 17 Jun 2011 |
| Total Posts: 3058 |
|
|
| 25 Jul 2012 09:09 PM |
@thek00lkid
are you stupid?
an object can have a parent, and then that parent can have a parent, and so forth.
"derp". |
|
|
| Report Abuse |
|
|
|
| 25 Jul 2012 09:10 PM |
It would allow you to check if it has a certain parent, and not to mention you wouldn't have to go doing "script.Parent.Parent.Parent.Parent" every time you want to index a parent.
and hi |
|
|
| Report Abuse |
|
|
|
| 25 Jul 2012 09:11 PM |
| @thek00lkid - Yeah, I know you're not stupid, but you should have read the whole post. :P |
|
|
| Report Abuse |
|
|
Cenort
|
  |
| Joined: 20 Nov 2009 |
| Total Posts: 9679 |
|
|
| 25 Jul 2012 09:14 PM |
model = game.Workspace.Model22
skateboard = script.Parent
--we need to find if skateboard is a descendent of model
print(model:IsAncestorOf(skateboard))
voila. |
|
|
| Report Abuse |
|
|
Strieken
|
  |
| Joined: 17 Jun 2011 |
| Total Posts: 3058 |
|
| |
|
|
| 25 Jul 2012 09:18 PM |
Oh, well, it's inefficient to have that many parents anyhow! XD
|
|
|
| Report Abuse |
|
|
|
| 25 Jul 2012 09:22 PM |
| @Cenort - That is irrelevant. That method tells if an object is an ancestor of another object. This function FINDS an ancestor of an object. |
|
|
| Report Abuse |
|
|
Cenort
|
  |
| Joined: 20 Nov 2009 |
| Total Posts: 9679 |
|
|
| 25 Jul 2012 09:24 PM |
| why would you know the name of the ancestor but not be able to access it?... very odd situation. |
|
|
| Report Abuse |
|
|
iAdobe
|
  |
| Joined: 22 May 2011 |
| Total Posts: 367 |
|
| |
|
|
| 25 Jul 2012 09:27 PM |
| What if you don't know that it's an ancestor? What if you don't know its name (variables)? What if you simply do not want to repeatedly type ".Parent.Parent.Parent"? |
|
|
| Report Abuse |
|
|
| |
|