CollectionService:HasTag accepts string instead of Instance and does not error with incorrect types

Repro:

print(game:GetService("CollectionService"):HasTag({}, "")) print("no error")

Output:

  02:15:37.075  > print(game:GetService("CollectionService"):HasTag({}, "")) print("no error")  -  Studio
  02:15:37.076  argument #1 expects a **string**, but **table** was passed  -  Edit
  02:15:37.076  Stack Begin  -  Studio
  02:15:37.076  Script 'print(game:GetService("CollectionService"):HasTag({}, "")) print("no error")', Line 1  -  Studio
  02:15:37.076  Stack End  -  Studio

When passing a string:

  02:15:54.805  > print(game:GetService("CollectionService"):HasTag("", "")) print("no error")  -  Studio
  02:15:54.806  false  -  Edit
  02:15:54.806  no error  -  Edit

When passing an Instance:

  02:16:06.583  > print(game:GetService("CollectionService"):HasTag(workspace, "")) print("no error")  -  Studio
  02:16:06.584  false  -  Edit
  02:16:06.584  no error  -  Edit

Expected behavior

CollectionService:HasTag’s first argument should expect an instance only but accepts a string or instance.

3 Likes

Hello!

This is an intentional behavior.

When calling with a string argument, you are calling:

CollectionService derives from an Instance. You are asking if the CollectionService has a tag on it. Extra arguments are ignored.

When calling with an instance and a string, you are calling:

Here you are asking the collection service if the instance has a tag.

1 Like

Ah wacky, I forgot about Instance:HasTag, however the behaviour of not erroring when a table is passed is really annoying and allowed a possible exploit in our game’s code where we assumed incorrect types would error if something different was passed in

We had plans to add an error in the past, but unfortunately it was not well received by developers relying on implicit conversions of values to strings in the API (we still allowed number->string as that was even more widely used).
We settled on displaying warnings to nudge developers, but reports show that while many experiences applied fixes, many remain with the issue.

1 Like