vlatkovski

findFirstChild (perfected)

Mar 30th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.14 KB | None | 0 0
  1. function findFirstChild(object, property, want, recursive)
  2.     local property, recursive, found = property or "Name", recursive or false, nil;
  3.     local function find(obj, prop, want)
  4.         for i,v in next, obj:GetChildren() do
  5.             local success, bool = pcall(function() return tostring(v[prop]) == tostring(want) end);
  6.             if (success and bool) then
  7.                 found = v;
  8.                 break;
  9.             elseif (recursive) then
  10.                 find(v, prop, want);
  11.             end
  12.         end
  13.     end
  14.     find(object, property, want);
  15.     return found;
  16. end
  17.  
  18. ----------------------------------------------------------------------
  19. ----------------------------------------------------------------------
  20.  
  21. Instance.new("StringValue", workspace).Value = "hey there!";
  22.  
  23. local foundValue = findFirstChild(workspace, "Value", "hey there!");
  24. print("FOUND: "..tostring(foundValue)..";\n   Value: "..foundValue.Value);
  25.  
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. --roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox roblox
Advertisement
Add Comment
Please, Sign In to add comment