CollectionService 인스턴스의 그룹(컬렉션)을 태그 로 관리합니다.태그는 서버에서 클라이언트로 복제되는 인스턴스에 적용되는 문자열 집합입니다.장소가 저장될 때도 직렬화됩니다.
CollectionService의 주요 사용은 동작을 확장하기 위해 사용할 수 있는 특정 태그로 인스턴스를 등록하는 것입니다.많은 다른 인스턴스에 동일한 스크립트를 추가하는 것을 발견하면 CollectionService 를 사용하는 스크립트가 더 나을 수 있습니다.
태그는 AddTag() 또는 RemoveTag()와 같은 이 클래스의 메서드를 통해 추가하거나 제거할 수 있습니다.스튜디오의 태그 섹션 또는 기본 제공 태그 편집기 도구를 통해 인스턴스 속성의 태그 섹션에서 직접 관리할 수도 있습니다.
복제
태그가 복제될 때, 인스턴스의 모든 태그가 동시에 복제됩니다. 따라서 클라이언트에서 인스턴스에 태그를 설정한 다음 서버에서 동일한 인스턴스에 다른 태그 를 추가/제거하면 인스턴스의 클라이언트 로컬 태그가 덮어씁니다.클라이언트의 스트리밍 영역을 떠날 때 인스턴스를 StreamingEnabled 장소에서 언로드할 수 있습니다.이러한 인스턴스가 스트리밍된 영역에 다시 들어가면 속성과 태그가 서버에서 다시 동기화됩니다.이로 인해 LocalScripts에 의해 만들어진 변경 사항이 덮어쓰거나 제거될 수 있습니다.
요약
메서드
태그를 Instance에 적용합니다.
경험의 모든 태그 배열을 반환합니다.
지정된 태그가 인스턴스에 추가될 때 발생하는 신호를 반환합니다.
지정된 태그가 인스턴스에서 제거될 때 발생하는 신호를 반환합니다.
지정된 태그로 게임에서 인스턴스 배열을 반환합니다.
지정된 인스턴스에 적용된 모든 태그의 배열을 가져옵니다.
인스턴스에 지정된 태그가 있는지 확인합니다.
인스턴스에서 태그를 제거합니다.
이벤트
태그가 인스턴스에 추가되고 추가된 태그가 해당 태그의 유일한 발생 플레이스경우 발생합니다.
태그가 인스턴스에서 제거되고 제거된 태그가 플레이스더 이상 사용되지 않을 때 발생합니다.
속성
메서드
AddTag
이 메서드는 태그를 Instance에 적용하여 태그가 이미 해당 인스턴스에 적용되는 경우 아무것도 수행하지 않습니다.태그를 성공적으로 추가하면 지정된 태그로 생성된 신호가 GetInstanceAddedSignal()에 발사됩니다.
경고
클라이언트 측에서 추가된 인스턴스의 태그는 서버가 나중에 해당 인스턴스에 태그를 추가하거나 제거하면 삭제됩니다, 서버는 모든 태그를 복제하고 이전 태그를 덮어쓰기 때문입니다.
인스턴스를 태그할 때 일부 리소스가 태그의 기능을 제공하기 위해 사용되는 것이 일반적입니다(예: 이벤트 연결 또는 테이블).메모리 누수를 방지하려면 태그가 더 이상 필요하지 않을 때 이를 정리하는 것이 좋습니다(연결 해제, nil).RemoveTag() , Instance:Destroy() 또는 GetInstanceRemovedSignal() 에서 반환된 신호에 연결된 함수에서 이를 수행하십시오.
매개 변수
반환
GetInstanceAddedSignal
태그(문자열)를 제공하면 이 메서드는 두 가지 조건에서 발생하는 신호를 반환합니다:
태그는 DataModel 또는 CollectionService:AddTag() 또는 Instance:AddTag()을 사용하여 인스턴스 내에 할당됩니다.The tag is assigned to an instance within the using 또는 .
지정된 태그를 가진 인스턴스가 DataModel의 하위로 추가되며, 예를 들어 Instance.Parent 또는 유사한 방식으로 설정합니다.
동일한 태그로 이 메서드에 대한 후속 호출은 동일한 신호 개체를 반환합니다.또한 태그가 이미 있는 인스턴스 목록을 가져오기 위해 를 호출하는 것도 고려하십시오(따라서 이미 발생한 이벤트를 발사하지 않을 것입니다).
또한 참조하십시오 GetInstanceRemovedSignal() 유사한 조건에서 발생하는 이벤트를 반환하는 것.
매개 변수
감시할 태그.
반환
태그를 인스턴스에 추가할 때 발생하는 이벤트입니다.
코드 샘플
This code sample causes any BasePart with the tag Deadly to kill any Humanoid that touches it. It does this using a common pattern with CollectionService to listen for all parts with the tag and make a connection, then disconnect the connection when the tag is removed.
local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
local connections = {}
local function onInstanceAdded(object)
-- Confirm that the object with this tag is a BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- If there is a stored connection on this object, disconnect/remove it
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
GetInstanceRemovedSignal
태그(문자열)를 제공하면 이 메서드는 두 가지 조건에서 발생하는 신호를 반환합니다:
태그는 DataModel 또는 CollectionService:RemoveTag() 또는 Instance:RemoveTag()을 사용하여 인스턴스 내에서 제거됩니다.
지정된 태그를 가진 인스턴스는 예를 들어 또는 유사한 방식으로 제거되어 하위로 이동됩니다.
동일한 태그로 이 메서드에 대한 후속 호출은 동일한 신호 개체를 반환합니다.신호는 연결 끊기와 같은 태그가 있던 인스턴스가 사용하는 리소스를 정리하는 데 유용합니다.
또한 참조하십시오 GetInstanceAddedSignal() 유사한 조건에서 발생하는 이벤트를 반환하는 것.
매개 변수
감시할 태그.
반환
태그를 인스턴스에서 제거할 때 발생하는 이벤트.
코드 샘플
This code sample causes any BasePart with the tag Deadly to kill any Humanoid that touches it. It does this using a common pattern with CollectionService to listen for all parts with the tag and make a connection, then disconnect the connection when the tag is removed.
local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
local connections = {}
local function onInstanceAdded(object)
-- Confirm that the object with this tag is a BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- If there is a stored connection on this object, disconnect/remove it
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
GetTagged
이 메서드는 DataModel의 후손인 특정 태그를 가진 인스턴스 배열을 반환합니다.CollectionService:RemoveTag() 또는 Instance:RemoveTag()을 사용하여 태그를 제거하면 이 메서드가 그들을 반환하지 않도록 합니다.
태그가 있는 모든 인스턴스를 감지하려면 와 미래 모두를 사용하여 인스턴스를 반복하고 GetInstanceAddedSignal()에서 반환된 신호에 연결하면서 이 메서드를 사용하여 인스턴스를 순환합니다.
이 메서드는 반환된 인스턴스의 순서를 보장하지 않습니다.또한 인스턴스에 지정된 태그가 할당될 수 있지만 DataModel의 하위가 아닐 수도 있으며, 예를 들어 부모가 nil인 경우 이 메서드는 그러한 인스턴스를 반환하지 않습니다.
매개 변수
검색할 태그.
반환
태그가 있는 모든 인스턴스 배열.
코드 샘플
This code sample causes any BasePart with the tag Deadly to kill any Humanoid that touches it. It does this using a common pattern with CollectionService to listen for all parts with the tag and make a connection, then disconnect the connection when the tag is removed.
local CollectionService = game:GetService("CollectionService")
local tag = "Deadly"
local function onDeadlyPartTouched(otherPart)
if not otherPart.Parent then
return
end
local humanoid = otherPart.Parent:FindFirstChildOfClass("Humanoid")
if humanoid then
humanoid.Health = 0
end
end
-- Save the connections so they can be disconnected when the tag is removed
local connections = {}
local function onInstanceAdded(object)
-- Confirm that the object with this tag is a BasePart
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- If there is a stored connection on this object, disconnect/remove it
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- Listen for this tag being applied to objects
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- Also detect any objects that already have the tag
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
end
GetTags
Instance 를 제공하면 이 메서드는 인스턴스에 적용된 태그의 배열을 반환합니다.
이 메서드는 한 번에 여러 인스턴스 태그로 작업하려는 경우 유용하지만, 단일 태그의 존재를 확인하는 것은 비효율적입니다.이를 위해 HasTag()를 사용하여 단일 태그를 확인하십시오.
매개 변수
태그가 반환되어야 하는 인스턴스입니다.
반환
지정된 인스턴스에 적용된 태그의 배열입니다.
코드 샘플
This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Add a tag
CollectionService:AddTag(object, "Deadly")
-- Query for a tag
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " is deadly")
end
-- List tags on an object
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
-- Remove a tag
CollectionService:RemoveTag(object, "Deadly")
HasTag
이 메서드는 지정된 Instance에 태그가 있는지 여부를 반환합니다.
확장으로, 인스턴스에서 GetTags()에 대한 호출로 반환된 모든 태그는 이 메서드와 함께 사용되면 true로 반환됩니다.
매개 변수
반환
인스턴스에 태그가 있는지 여부.
코드 샘플
This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Add a tag
CollectionService:AddTag(object, "Deadly")
-- Query for a tag
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " is deadly")
end
-- List tags on an object
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
-- Remove a tag
CollectionService:RemoveTag(object, "Deadly")
RemoveTag
이 메서드는 인스턴스에서 태그를 제거합니다. 태그를 성공적으로 제거하면 GetInstanceRemovedSignal()에서 지정한 태그로 생성된 신호가 발생합니다.
태그를 제거할 때 일부 리소스가 태그의 기능을 제공하기 위해 사용되는 것이 일반적입니다(예: 이벤트 연결 또는 테이블).메모리 누수를 방지하려면 태그가 더 이상 필요하지 않을 때 이를 정리하는 것이 좋습니다(연결 해제, nil).
매개 변수
반환
코드 샘플
This code sample demonstrates adding, removing and querying a tag from an object using CollectionService.
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- Add a tag
CollectionService:AddTag(object, "Deadly")
-- Query for a tag
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " is deadly")
end
-- List tags on an object
local tags = CollectionService:GetTags(object)
print("The object " .. object:GetFullName() .. " has tags: " .. table.concat(tags, ", "))
-- Remove a tag
CollectionService:RemoveTag(object, "Deadly")