Advertisement
CluelessDev

FindTagged

Oct 14th, 2022
859
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.85 KB | None | 0 0
  1. type array<T> = {number: T}
  2.  
  3. local CollectionService = game:GetService("CollectionService")
  4.  
  5. local module = {}
  6.  
  7. local function handleException(parent, tag)
  8.     assert(typeof(parent) == "Instance", tostring(parent).."Is not of type Instance")
  9.     assert(typeof(tag) == "string", tostring(tag).." Is not a string")
  10. end
  11.  
  12.  
  13. -- Returns the first descendant with the given tag within the given parent
  14. module.Descendant = function(parent: Instance, tag: string): Instance
  15.     handleException(parent, tag)
  16.     for _, child: Instance in parent:GetDescendants() do
  17.         if CollectionService:HasTag(child, tag) then
  18.             return child
  19.         end
  20.     end
  21. end
  22.  
  23. -- Returns the first descendant of the given class with the given class within the given parent
  24. module.DescendantOfClass = function(parent: Instance, tag: string, className): Instance
  25.     handleException(parent, tag)
  26.     for _, descendant: Instance in parent:GetDescendants() do
  27.         if CollectionService:HasTag(descendant, tag) and descendant.ClassName == className then
  28.             return descendant
  29.         end
  30.     end
  31. end
  32.  
  33.  
  34.  
  35.  
  36.  
  37. -- Returns an array of all descendants with the given tag within the given parent
  38. module.Descendants = function(parent: Instance, tag: string): Array<Instance>
  39.     handleException(parent, tag)
  40.    
  41.     local taggedDescendants = {}
  42.  
  43.     for _, descendant: Instance in parent:GetDescendants() do
  44.         if CollectionService:HasTag(descendant, tag) then
  45.             table.insert(taggedDescendants, descendant)
  46.         end
  47.     end
  48.  
  49.     return taggedDescendants
  50. end
  51.  
  52. -- Returns an array of all descendants of the given class with the given tag within the given parent
  53. module.DescendantsOfClass = function(parent: Instance, tag: string, className): Array<Instance>
  54.     handleException(parent, tag)
  55.  
  56.     local taggedDescendantsOfGivenClass = {}
  57.  
  58.     for _, descendant: Instance in parent:GetDescendants() do
  59.         if CollectionService:HasTag(descendant, tag) and descendant.ClassName == className then
  60.             table.insert(taggedDescendantsOfGivenClass, descendant)
  61.         end
  62.     end
  63.  
  64.     return taggedDescendantsOfGivenClass
  65. end
  66.  
  67.  
  68.  
  69.  
  70.  
  71. -- Returns the first child with the given tag within the given parent
  72. module.Child = function(parent: Instance, tag: string): Instance
  73.     handleException(parent, tag)
  74.  
  75.     for _, child: Instance in parent:GetChildren() do
  76.         if CollectionService:HasTag(child, tag) then
  77.             return child
  78.         end
  79.     end
  80. end
  81.  
  82. -- Returns the first descendant of the given class with the given tag within the given parent
  83. module.ChildOfClass = function(parent: Instance, tag: string, className): Instance
  84.     for _, child: Instance in parent:GetChildren() do
  85.         if CollectionService:HasTag(child, tag) and child.ClassName == className then
  86.             return child
  87.         end
  88.     end
  89. end
  90.  
  91.  
  92.  
  93.  
  94.  
  95. -- Returns an array of all children with the given tag within the given parent
  96. module.Children = function(parent: Instance, tag: string): Array<Instance>
  97.     handleException(parent, tag)
  98.  
  99.     local taggedChildren = {}
  100.  
  101.     for _, child: Instance in parent:GetChildren() do
  102.         if CollectionService:HasTag(child, tag) then
  103.             table.insert(taggedChildren, child)
  104.         end
  105.     end
  106.  
  107.     return taggedChildren
  108. end
  109.  
  110. -- Returns an array of all children of the given class with the given tag within the given parent
  111. module.ChildrenOfClass = function(parent: Instance, tag: string, className): Array<Instance>
  112.     handleException(parent, tag)
  113.  
  114.     local taggedChildrenOfGivenClass = {}
  115.  
  116.     for _, child: Instance in parent:GetChildren() do
  117.         if CollectionService:HasTag(child, tag) and child.ClassName == className then
  118.             table.insert(taggedChildrenOfGivenClass, child)
  119.         end
  120.     end
  121.  
  122.     return taggedChildrenOfGivenClass
  123. end
  124.  
  125.  
  126.  
  127. return module
  128.  
  129.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement