Instance:HasTag() argument 2 missing or nil

Title is self-explanatory. Trying to use Instance:HasTag() directly on an Instance succeeds, but in the context of running a loop over some instances to see if any one Instance has a specific tag, the loop will succeed and then error. This is somewhat confusing and peculiar behavior that I don’t understand.

has-tag-argument-2-missing-or-nil-idk-bruh.rbxl (52.3 KB)

EDIT: I did some further investigation to print what Instance it was that was causing this error and it ends up being CollectionService.

3 Likes

Seems like CollectionService has a HasTag of its own!


It isn’t really a bug, more like a function-name overlap issue.
This is also interesting to see the priority given for super-class functions.

2 Likes

The method call you want here is CollectionService:GetTagged(<tag>), which will not run into this issue and be much more efficient than what you were trying to do as a bonus.

2 Likes

Hi,

Yes, I know. I’m reporting this engine bug specifically because it errors when I try to use it in this instance. I know it seems superfluous, but a bug is a bug

2 Likes

I wouldn’t necessarily say gettagged is a better way of checking if something has a tag before runtime. Especially in the command bar, because you’d have to add a for loop in a for loop as it returns an array of objects. Whereas hastag will just print the object that has the tag. Much more simpler. A good way to check if something in the whole game has a tag.

1 Like

I don’t know if you figured out the solution yet as I don’t have the best english but the documentation says hastag requires two arguments, the first one is the instance you need to check and the second is the name. (If you do it through collectionservice)

However if you just do Instance:HasTag(“Name”) it only requires one argument, and that’s just the name.

1 Like

Fair, you could early-out on some combination of HasTag checks.

If you just want to find out whether anything has a given tag there’s also a better way to do that by calling table.find(CollectionService:GetAllTags(), <tag>).

1 Like

Yes that is also true, I personally find the first way alot easier to do but we all script differently I guess.

Also thank you for staying respectful, the forum can sometimes have quite heated arguments.

1 Like

I think a good implementation would be sort of like this in the bar:

print(table.find(CollectionService:GetAllTags(), <tag>))

Very simple actually, good suggestion. I take my word back, much easier. Very clever way of using collectionservice mr staff.

1 Like