An alternative to chaining WaitForChilds

Unless you’re tomarty and play god with the objects in your game, you chain WaitForChilds (or implement wrappers, as some did in that thread). I think I came up with an easy alternative that might be better?

local CollectionService = game:GetService("CollectionService")

local function CollectObject(tag)
    local tagged = CollectionService:GetTagged(tag)
    if tagged then
        return tagged[1]
    else
        return CollectionService:GetInstanceAddedSignal(tag):Wait()
    end
end

This uses CollectionService to get an object with a unique tag or wait until an object is given the tag. The key would be to also write a plugin that automatically parses your code for CollectObject calls and tags the objects.

So is this stupid? As far as I know, there are no repercussions for having a zillion tags.

EDIT: Yeah this is dumb

I believe this would be not much different for :WaitForChild(), there is no reason to recreate :WaitForChild(). Only recently, have I figured out that :WaitForChild() allows a second argument which is an automatic timeout.

Realistically you should almost always retrieve child instances through chaining method calls. Also, you typically have to assert that the retrieved instance exists before calling some getter method on that.

Chaining WaitForChild isn’t too bad, but opt for FindFirstX methods when you don’t have to yield for the item when possible.

On that same note, I find that most helper functions like your post provides are more cumbersome than helpful in projects.