エンジンクラス
CollectionService
*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。
概要
方法
GetCollection(class: string):Instances |
イベント
ItemAdded(instance: Instance):RBXScriptSignal |
ItemRemoved(instance: Instance):RBXScriptSignal |
TagAdded(tag: string):RBXScriptSignal |
TagRemoved(tag: string):RBXScriptSignal |
APIリファレンス
方法
AddTag
GetCollection
GetInstanceAddedSignal
パラメータ
コードサンプル
コレクションサービスを使用したデッドリー・ブリックス
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
-- タグが削除されたときに接続を切断できるように、接続を保存する
local connections = {}
local function onInstanceAdded(object)
-- このタグを持つオブジェクトが BasePart であることを確認する
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- このオブジェクトに保存された接続がある場合、それを切断/削除する
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- オブジェクトにこのタグが適用されるのをリッスンする
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- すでにタグを持つオブジェクトも検出する
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
endGetInstanceRemovedSignal
パラメータ
コードサンプル
コレクションサービスを使用したデッドリー・ブリックス
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
-- タグが削除されたときに接続を切断できるように、接続を保存する
local connections = {}
local function onInstanceAdded(object)
-- このタグを持つオブジェクトが BasePart であることを確認する
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- このオブジェクトに保存された接続がある場合、それを切断/削除する
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- オブジェクトにこのタグが適用されるのをリッスンする
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- すでにタグを持つオブジェクトも検出する
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
endGetTagged
パラメータ
戻り値
Instances
コードサンプル
コレクションサービスを使用したデッドリー・ブリックス
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
-- タグが削除されたときに接続を切断できるように、接続を保存する
local connections = {}
local function onInstanceAdded(object)
-- このタグを持つオブジェクトが BasePart であることを確認する
if object:IsA("BasePart") then
connections[object] = object.Touched:Connect(onDeadlyPartTouched)
end
end
local function onInstanceRemoved(object)
-- このオブジェクトに保存された接続がある場合、それを切断/削除する
if connections[object] then
connections[object]:Disconnect()
connections[object] = nil
end
end
-- オブジェクトにこのタグが適用されるのをリッスンする
CollectionService:GetInstanceAddedSignal(tag):Connect(onInstanceAdded)
CollectionService:GetInstanceRemovedSignal(tag):Connect(onInstanceRemoved)
-- すでにタグを持つオブジェクトも検出する
for _, object in pairs(CollectionService:GetTagged(tag)) do
onInstanceAdded(object)
endGetTags
パラメータ
戻り値
コードサンプル
タグと CollectionService の使用
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- タグを追加
CollectionService:AddTag(object, "Deadly")
-- タグをクエリ
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " は致命的です")
end
-- オブジェクトのタグをリスト
local tags = CollectionService:GetTags(object)
print("オブジェクト " .. object:GetFullName() .. " のタグ: " .. table.concat(tags, ", "))
-- タグを削除
CollectionService:RemoveTag(object, "Deadly")HasTag
戻り値
コードサンプル
タグと CollectionService の使用
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- タグを追加
CollectionService:AddTag(object, "Deadly")
-- タグをクエリ
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " は致命的です")
end
-- オブジェクトのタグをリスト
local tags = CollectionService:GetTags(object)
print("オブジェクト " .. object:GetFullName() .. " のタグ: " .. table.concat(tags, ", "))
-- タグを削除
CollectionService:RemoveTag(object, "Deadly")RemoveTag
戻り値
()
コードサンプル
タグと CollectionService の使用
local CollectionService = game:GetService("CollectionService")
local Workspace = game:GetService("Workspace")
local object = Workspace.Part
-- タグを追加
CollectionService:AddTag(object, "Deadly")
-- タグをクエリ
if CollectionService:HasTag(object, "Deadly") then
print(object:GetFullName() .. " は致命的です")
end
-- オブジェクトのタグをリスト
local tags = CollectionService:GetTags(object)
print("オブジェクト " .. object:GetFullName() .. " のタグ: " .. table.concat(tags, ", "))
-- タグを削除
CollectionService:RemoveTag(object, "Deadly")イベント
ItemAdded
ItemRemoved