CollectionService
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
CollectionService は、 タグ を持つインスタンスのグループ (コレクション) を管理します。タグは、サーバーからクライアントにレプリケートするインスタンスに適用される文字列のセットです。場所が保存されるときにもシリアライズされます。
CollectionService の主な使用は、動作を拡張するために使用できる特定のタグを持つインスタンスを登録することです。同じスクリプトを多くの異なるインスタンスに追加している自分を見つけた場合、CollectionService を使用するスクリプトがより良いかもしれません。
タグは、AddTag() または RemoveTag() などのこのクラスのメソッドを通じて追加または削除できます。また、インスタンスのプロパティの タグ セクションまたは内蔵の タグエディタ ツールを通じて、直接Studioで管理することもできます。
レプリケーション
タグが複製されると、 インスタンス上のすべてのタグが同時に複製されます 。たとえば、クライアントからインスタンスにタグを設定し、サーバーから同じインスタンスに 異なる タグを追加/削除すると、インスタンスのローカルタグが上書きされる。StreamingEnabled 場所で、インスタンスは、クライアントのストリームエリアを離れると、ロードが解除されます。そのようなインスタンスがストリームされた領域に再入る場合、プロパティとタグはサーバーから再同期されます。これにより、LocalScripts によって行われた変更が上書き/削除される可能性があります。
概要
方法
タグを Instance に適用します。
エクスペリエンス内のすべてのタグの配列を返します。
指定されたタグがインスタンスに追加されたときに発射するシグナルを返します。
指定されたタグがインスタンスから削除されたときに発射するシグナルを返します。
指定されたタグを持つゲーム内のインスタンスのアレイを返します。
指定されたインスタンスに適用されたすべてのタグの配列を取得します。
インスタンスに指定されたタグがあるかどうかをチェックします。
インスタンスからタグを削除します。
イベント
タグがインスタンスに追加され、追加されたタグがそのタグの場プレースでの唯一の発生であるときに発火します。
タグがインスタンスから削除され、削除されたタグが場プレースのどこでも使用されなくなったときに発火します。
プロパティ
方法
AddTag
このメソッドは、タグを Instance に適用し、タグがすでにそのインスタンスに適用されている場合は何も行わない。タグを成功裏に追加すると、指定されたタグで GetInstanceAddedSignal() によって作成されたシグナルが発射されます。
注意
サーバーが後で、サーバーがすべてのタグを複製して前のタグを上書きするため、クライアント側に追加されたインスタンスのタグが削除されます。An instance's tags that were added client-side will be dropped if the server later adds or removes a tag on that instance because the server replicates all tags together and overwrites previous tags.
インスタンスをタグ付けするとき、タグの機能を提供するために、例えばイベント接続またはテーブルが使用されるのは一般的です。メモリ漏れを防ぐには、タグがもう必要ないときにこれらをクリーンアップする (接続を切断し、nil に設定など) が良い考えです。RemoveTag() 、Instance:Destroy() 、または GetInstanceRemovedSignal() によって返されたシグナルに接続された関数でこれを行うとき。
パラメータ
戻り値
GetInstanceAddedSignal
タグ (文字列) を与えると、このメソッドは 2つの条件の下で発動するシグナルを返します:
タグは DataModel または CollectionService:AddTag() または Instance:AddTag() を使用してインスタンスに割り当てられます。
指定されたタグを持つインスタンスが、例えば DataModel または同様に Instance.Parent を設定して、子孫として追加されます。
同じタグでこのメソッドに後続の呼び出しは、同じシグナルオブジェクトを返します。また、GetTagged() を呼び出して、すでにタグを持っているインスタンスのリストを取得することも考えてください (そして、すでに DataModel に含まれている場合はイベントを発動しません)。
同様の条件で発動するイベントを返す 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
タグ (文字列) を与えると、このメソッドは 2つの条件の下で発動するシグナルを返します:
タグは 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")