Search+ | Added Instance Search Functions

Search+

Expanded Instance Searching

Overview

Search+ is a module I designed to do one simple task: extend on the default Lua Instance functions (e.g. Instance:GetChildren). They work well enough for most cases, but what if they just don't cut it?

Search+ is a module designed to do just that: supplement the built-in searches with things such as GetChildrenWithTag, FindFirstDescendantWithTag, and more. All documentation is provided in this post.


Module & Installation

The module can be found here, but I recommend doing something like below to get the most up-to-date version of Search+, as I will be updating this module frequently. As mentioned by @LucasTutoriaisSaimo, I recommend you go to the link.
local SearchPlus = require(6715385532)

Documentation

function SearchPlus:GetDescendantsWithTag(instance, tag)
Parameters:

  • instance Instance: the Instance Search+ will search.
  • tag string: the CollectionService tag to use.

Returns:

  • result: an Array containing all the Instances that have the passed tag and descend from the provided Instance.

function SearchPlus:GetChildrenWithTag(instance, tag)
Parameters:

  • instance Instance: the Instance Search+ will search.
  • tag string: the CollectionService tag to use.

Returns:

  • result: an Array containing all the Instances that have the passed tag and are direct children from the provided Instance.

function SearchPlus:FindFirstChildWithTag(instance, tag)
Parameters:

  • instance instance: the Instance Search+ will search in.
  • tag string: the CollectionService tag to use.

Returns:

  • result Instance: the first Instance that is a direct child of the provided Instance and has the passed tag. If none are found, returns nil.

function SearchPlus:FindFirstDescendantWithTag(instance, tag)
Parameters:

  • instance Instance: the Instance Search+ will use.
  • tag string: the CollectionService tag to use.

Returns:

  • result Instance: the first Instance that is descended from the provided Instance and has the passed tag. If none are found, returns nil.

Changelog

V1.00

Initial release, with 4 functions.

Thank you for reading, and I hope you find this useful!

How useful was this module?
  • 1
  • 2
  • 3
  • 4
  • 5

0 voters

6 Likes

By the way, I would not recommend like advertising the fact that you can require the ID, it causes a lot of issues, a LOT of times, and for anyone reading this, try to just stay away from requiring by ID, it’s not the best choice out there.

Viewing the source code, it’s fine, I don’t see anything wrong with your code, it’s more of a utility module, that’s fine.

One problem while looking at the source code I found though, is that you’re returning nil, and :/, just fix that and you’re fine.

image

With that, some things you can do, since you’re gonna fix the returning anyway, I would recommend you to make it so that it redirects to CollectionService.

That’s not hard to do, here’s a little tutorial for you on how to do it.

image
Have a __index metamethod which is equal to CollectionService, any time someone indexes something that doesn’t exist on your module, it will try to find it in CollectionService instead.

Now, you need to make it into a metatable, so you can just return setmetatable(SearchPlus, SearchPlus) since setmetatable returns the table you added a metatable to.

image

And done, that’s what I would do to make it better.

Aren’t metatables less performant? Also, what do you imply by:

I thought I already stored CS in a local.
Please forgive me if I’m wrong, I’m not the best programmer.

By redirecting I mean that you can use CollectionService directly by your module instead of people having to get another variable for CollectionService.

Don’t worry about performance with metatables. It’s only like 0.00000000000001% slower and only when it’s trying to get something that doesn’t exist in your module.

Applying what I said, you would be able to use CollectionService functions directly from your module.

2 Likes