CollectionService Tag API does not error when nil is given as the instance argument

Issue Type: Other
Impact: Low
Frequency: Very Rare
Date First Experienced: 2021-04-14 16:45:00 (-03:00)
Date Last Experienced:

Reproduction Steps:

  1. Open an empty baseplate
  2. Create a script with the following code and run it (or alternatively run the following code in the command bar)
-- No errors are thrown in the following lines
game.CollectionService:AddTag(nil, "Hello")
print(game.CollectionService:HasTag(nil, "Hello")) -- > false
game.CollectionService:RemoveTag(nil, "Hello")
  1. Open the output. No errors have been thrown, and false from the third line is printed.

Expected Behavior:
Since it’s invalid to assign a tag to nil, I expected the functions to error. I came across this bug while debugging my own code, when I realized that I was mistakenly passing a typo-ed variable (which didn’t exist, so it resolved to nil) to CollectionService:AddTag() when I intended to pass a valid instance value instead. Note that if this is fixed, there is a chance that scripts which did not error before will no longer work. Passing other value types such as numbers and booleans return errors as normal.

Actual Behavior:
CollectionService tag-related APIs such as HasTag, AddTag and RemoveTag do not throw errors and silently fail when an invalid instance reference, nil, is given. This was tested on Studio Version 0.473.0.420291 (64bit) and in game and it happens in both.

Workaround:

1 Like

I’m pretty sure this is intended behavior Debris.AddItem is also like this.

Imagine a scenario where you AddTag on something like Table.Instance but Table.Instance was removed a nanosecond before AddTag was executed.

Or using something like FindFirstChild

If you want to prevent mistakes that you mentioned you can use t library or create a function that checks if the argument is an Instance or use a better Script Editor

Thanks for the report! We’ve filed a ticket to our internal database and we’ll follow up when we have an update for you.

3 Likes

It is now possible to use this API directly on the instance itself so it is unlikely we will make this change but thank you for submitting this report.

It is now possible to use this API directly on the instance itself so it is unlikely we will make this change but thank you for submitting this report.

I just wanted to add a bit of extra commentary. The new API gives the error reporting you are after since calling a method on a nil instance will generate an error.

While it could be beneficial to also have this error reporting on the older CollectionService API, we have concerns that existing code may intentionally or unintentionally be relying on the current behavior. So adding error reporting could break existing experiences.

2 Likes