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.

3 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

Closing this issue because this is basically how overridden methods work in any language. If you want this to work normally you must store a copy of the HasTag from an instance.

local hasTag = workspace.HasTag
hasTag(game.CollectionService, "Blah")

Note this will also deoptimize your code, and so using the above solutions is a much better idea. But yes, this is how Lua works, and is not technically a bug, although understandably confusing.

Thank you for the report!

1 Like

In this context do you mean ‘deoptimize’ as-in it’s just not the best solution, or is a lua(u) optimization being disabled here?

I mean, Luau has a special op-code to run instance:Method() calls, and by keeping a reference to the method instead of immediately invoking it, you make 2 calls to the Lua VM instead of 1.

In reality this doesn’t make a big difference, especially if running in a command line. However, it’s technically interesting and worth thinking about.

Overall, the solutions presented above are much better than my proposed solution. My proposed solution is just a minimum change you’d need to actually get stuff working.

2 Likes