Qorm
|
  |
| Joined: 25 Jul 2010 |
| Total Posts: 1650 |
|
|
| 05 May 2016 11:38 PM |
function findPart(startPoint, objectToFind) --object, string if startPoint.Name == objectToFind then print"yay" return true end for _, v in pairs(startPoint:GetChildren()) do findPart(v, objectToFind) end end
if findPart(script.Parent, "Part") then print"a" else print"b" end
output: yay b
it finds the part and returns true but the if statement says otherwise
|
|
|
| Report Abuse |
|
|
|
| 05 May 2016 11:46 PM |
Oh, I think what's happening is it's returning the function to itself with the recursive call.
I guess you have to keep returning up the stack trace to the original function?
|
|
|
| Report Abuse |
|
|
|
| 05 May 2016 11:47 PM |
Ye, ur missing a return When u call findPart from the for loop, you have to return the result of that |
|
|
| Report Abuse |
|
|
|
| 05 May 2016 11:48 PM |
....although its a for loop. So keep a variable, if that variable gets set to true then you return |
|
|
| Report Abuse |
|
|
|
| 05 May 2016 11:48 PM |
Oh, nevermind, it's not recursive, it's only a static level deep. So just return from THAT.
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 06 May 2016 12:01 AM |
if script.Parent:FindFirstChild("Part",true) then print("Found part") else print("No part exists") end
|
|
|
| Report Abuse |
|
|
Qorm
|
  |
| Joined: 25 Jul 2010 |
| Total Posts: 1650 |
|
|
| 06 May 2016 12:06 AM |
@TimeTicks, same issue
function findPart(startPoint, objectToFind) --object, string if startPoint.Parent:findFirstChild(objectToFind) then print"yay" return true end for _, v in pairs(startPoint:GetChildren()) do findPart(v, objectToFind) end end
if findPart(script.Parent, "Part") then print"a" else print"b" end
output:
yay b
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 06 May 2016 12:08 AM |
Qorm, what I was saying is your function is stupid and useless, just use findfirstchild with recursion.
|
|
|
| Report Abuse |
|
|
Qorm
|
  |
| Joined: 25 Jul 2010 |
| Total Posts: 1650 |
|
|
| 06 May 2016 12:09 AM |
i need a recursion because what if i need to search through multiple parts and models etc
|
|
|
| Report Abuse |
|
|
Qorm
|
  |
| Joined: 25 Jul 2010 |
| Total Posts: 1650 |
|
|
| 06 May 2016 12:10 AM |
oh well could i have an example plz
|
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 06 May 2016 12:10 AM |
...Findfirstchild already has that
http://wiki.roblox.com/index.php?title=API:Class/Instance/FindFirstChild
|
|
|
| Report Abuse |
|
|
Qorm
|
  |
| Joined: 25 Jul 2010 |
| Total Posts: 1650 |
|
|
| 06 May 2016 12:12 AM |
wwwwwwwwwwwwwttttttttttffffffffffffffffffffffffffffffffffffffffff wow thanks
|
|
|
| Report Abuse |
|
|