instanceTagUtil
A utility module for working with instance tags.
Functions
observe
Observes the given CollectionService tag.
tagAddedCallback
will be automatically called on all instances with the given tag
, and tagRemovedCallback
(if specified)
will be automatically called, being passed the instance that no no longer has the given tag
.
instanceTagUtil.observe("SomeTag", function(instanceWithTheTag)
end, function(instanceThatHadTheTag)
end)
instanceTagPromise
Returns a promise which is resolved when the given instance has the given tag
.
NOTE
The returned promise will be cancelled if instance
is destroyed.
instanceTagUtil.instanceTagPromise(instance, "SomeTag", function()
print(instance.Name, "now has the tag 'SomeTag'")
end)
addTagsToInstance
Adds all tags in tags
to the given instance
.
instanceTagUtil.addTagsToInstance(workspace.Baseplate, {"Test"})
print(CollectionService:HasTag(workspace.Baseplate, "Test")) --> true
removeTagsFromInstance
Removes all tags in tags
from the given instance
.
instanceTagUtil.addTagsToInstance(workspace.Baseplate, {"Test"})
print(CollectionService:HasTag(workspace.Baseplate, "Test")) --> true
instanceTagUtil.removeTagsFromInstance(workspace.Baseplate, {"Test"})
print(CollectionService:HasTag(workspace.Baseplate, "Test")) --> false
observeMany
Works similar to instanceTagUtil.observe, but observes an array of tags instead of a single tag. observer
will initially be immediately called being passed an array of all the tags (in tagsToObserve
) that the given
instance
has at that time, and then called again everytime a tag (that exists in tagsToObserve
) is added or removed
from the given instance
.
oldTags
will be initially nil
the first time observer
is called. However whenever the observer
is called again,
oldTags
will be be an array representing the tags the instance
had during the time when observer
was previously
called.
instanceTagUtil.observeMany(workspace.Part, {"Number1", "Number2"}, function(newTags, oldTags)
print(`newTags: {newTags}, oldTags: {oldTags}`)
end)
workspace.Part:AddTag("Number1")
workspace.Part:AddTag("Number2")
-- OUTPUT:
--> {}, nil
--> {"Number1", "Number2"}, {}