TagService
Queries tagged instances and provides tag change signals.
- Service
game:GetService("TagService")- Inherits
- Instance
- Replication
- Never
Behavior
TagService queries instances by string tag, regardless of their parent or class.
Instances own their tags. The service provides queries and signals for tagged
sets.
Connect to the added signal before the initial GetTagged query to track
additions during setup. Make setup safe to repeat for the same instance.
Summary
Whether the service instance is included when its containing hierarchy is cloned or saved.
The instance name.
The service's parent instance.
Returns a signal that fires when an instance gains the specified tag.
Returns an array of tag names known to the service.
Returns this instance if it is an ExecutionGroup.
Returns a signal that fires when an instance loses the tag or leaves the hierarchy.
Returns an array of instances carrying the specified tag.
Properties
Archivable
TagService.Archivable: boolean = trueWhether the service instance is included when its containing hierarchy is cloned
or saved. The default is true.
Name
TagService.Name: string = "TagService"The instance name. The default is "TagService".
Parent
TagService.Parent: Instance? = nilThe service's parent instance. The default is nil.
Methods
GetAddedSignal
TagService:GetAddedSignal(tag: string): AstScriptSignalReturns a signal that fires when an instance gains the specified tag.
GetAllTags
TagService:GetAllTags(): tableReturns an array of tag names known to the service.
GetExecutionGroup
TagService:GetExecutionGroup(): ExecutionGroup?Returns this instance if it is an ExecutionGroup. Otherwise returns the nearest ancestor ExecutionGroup, or nil when no group contains the instance.
GetRemovedSignal
TagService:GetRemovedSignal(tag: string): AstScriptSignalReturns a signal that fires when an instance loses the tag or leaves the hierarchy.
GetTagged
TagService:GetTagged(tag: string): tableReturns an array of instances carrying the specified tag. Includes instances outside workspace.
Inherited members
- Properties
- ClassName
- Methods
- AddComponent()AddTag()ClearAllChildren()ClearTags()Clone()Destroy()FindFirstAncestor()FindFirstAncestorOfClass()FindFirstAncestorWhichIsA()FindFirstChild()FindFirstChildOfClass()FindFirstChildWhichIsA()FindFirstDescendant()GetAttribute()GetAttributeChangedSignal()GetAttributes()GetChildren()GetComponent()GetComponentChangedSignal()GetComponents()GetDebugId()GetDescendants()GetFullName()GetPropertyChangedSignal()GetReferencing()GetTags()HasComponent()HasTag()IsA()IsAncestorOf()IsDescendantOf()RemoveComponent()RemoveTag()SetAttribute()WaitForChild()
- Events
- AncestryChangedAttributeChangedChangedChildAddedChildRemovedDescendantAddedDescendantRemovingDestroying
Examples
local TagService = game:GetService("TagService")
for _, instance in TagService:GetTagged("Checkpoint") do
print(instance.Name)
end-- Server Script. Records contacts with Parts tagged Checkpoint.
local TagService = game:GetService("TagService")
local Players = game:GetService("Players")
local TAG = "Checkpoint"
-- Keep checkpoint state per player across character respawns.
local reached = {}
local watched = setmetatable({}, { __mode = "k" })
local function onTouched(pad, hit)
local character = hit.Parent
local player = character and Players:GetPlayerFromCharacter(character)
if not player then
return
end
if reached[player] == pad then
return
end
reached[player] = pad
print(player.Name .. " reached " .. pad.Name)
end
local function watch(pad)
if not pad:IsA("BasePart") or watched[pad] then
return
end
watched[pad] = true
pad.Touched:Connect(function(hit)
onTouched(pad, hit)
end)
end
TagService:GetAddedSignal(TAG):Connect(watch)
for _, pad in TagService:GetTagged(TAG) do
watch(pad)
end
Players.PlayerRemoving:Connect(function(player)
reached[player] = nil
end)