Instance

非推奨を表示

*このコンテンツは、ベータ版のAI(人工知能)を使用して翻訳されており、エラーが含まれている可能性があります。このページを英語で表示するには、 こちら をクリックしてください。

作成できません
閲覧できません

Instance は、DataModel ツリーの一部である Roblox クラス階層のすべてのクラスのベースクラスです。

直接ルート Instance オブジェクトを作成することはできませんが、特別な Instance.new() コンストラクターはコードを介してオブジェクトを作成し、クラスの名前をパラメータとして取り、作成されたオブジェクトを返します。

概要

プロパティ

  • 並列読み取り

    Instance とその子孫が Instance:Clone() を使用してクローンでき、保存/公開できるかどうかを決定します。

  • Capabilities:SecurityCapabilities
    並列読み取り

    このコンテナ内のスクリプトで使用できる機能のセット。

  • 並列読み取り

    非唯一の識別子 Instance

  • 複製されていません
    並列読み取り

    Instance の階層親を決定します。

  • 非表示
    プラグインのセキュリティ
    並列読み取り
    非推奨

    CoreGui オブジェクトを保護するのに使用されていた廃止されたプロパティ。

  • 複製されていません
    並列読み取り

    インスタンスをサンドボックス化されたコンテナに変えます。

  • UniqueId:UniqueId
    複製されていません
    スクリプト作成できません
    Robloxのセキュリティ
    並列読み取り

    インスタンスのユニークな識別子。

方法

イベント

プロパティ

Archivable

並列読み取り

このプロパティは、インスタンスをエクスペリエンスが公開または保存されるとき、または Clone() がインスタンスの祖の 1 つに呼び出されるときに含めるべきかどうかを決定します。インスタンスに直接 を呼び出すと、そのインスタンスが でない場合、 が返されます。

スタジオで 複製 または コピー / 貼り付け オプションを使用してオブジェクトをコピーすると、自分の プロパティを無視し、コピーに を設定します。


local part = Instance.new("Part")
print(part:Clone()) --> Part
part.Archivable = false
print(part:Clone()) --> nil

Capabilities

SecurityCapabilities
並列読み取り

このインスタンス内のスクリプトで使用できる機能のセット。機能が有効になるには、Instance.Sandboxed プロパティを有効にする必要があります。

このプロパティは、実験機能によって使用されています。詳細は、スクリプト機能 を参照してください。

Name

並列読み取り

非唯一の識別子 Instance 。名前は、オブジェクトの階層を整理し、スクリプトが特定のオブジェクトにアクセスできるようにするとともに使用されます。インスタンスの名前はサイズで 100 文字を超えることはできません。

オブジェクトの名前は、次の方法を使用してデータモデル階層を介してオブジェクトにアクセスするためによく使用されます:


local Workspace = game:GetService("Workspace")
local baseplate = Workspace.Baseplate
local baseplate = Workspace["Baseplate"]
local baseplate = Workspace:FindFirstChild("BasePlate")

ドット演算子 ( . ) を使用してオブジェクトにアクセスするには、名前はアンダースコアまたは文字で始まり、残りの名前は文字、数字、またはアンダースコア (他の特殊文字は含まれない) のみを含める必要があります。オブジェクトの名前がこの構文に従わない場合、ドット演算子を使用してアクセスできず、Luauはその名前を識別子として解釈しません。

同じ名前を持つ複数のオブジェクトが兄弟である場合、その名前でオブジェクトをインデックスに登録する試みは、Instance:FindFirstChild() のように、1つのオブジェクトだけを返すが、必ずしも望んでいるオブジェクトではない。特定のオブジェクトにコードを介してアクセスする必要がある場合、ユニークな名前を付与するか、同じ名前を共有しないことを保証することをお勧めします。

も参照してください Instance:GetFullName() オブジェクトの階層を含む全名を取得するには。

Parent

複製されていません
並列読み取り

The Parent プロパティは、Instance の階層上の親を決定します。次の用語は、このプロパティが設定される方法について話すときによく使用されます:

  • オブジェクトは、 子供 であるか、 になり、その Parent がそのオブジェクトに設定されると、他のオブジェクトになります。

  • 子孫 は、Instance の子孫であり、子孫の子孫も含まれます。

  • インスタンスが子孫であるすべてのオブジェクトは、 祖先 です。

それは Parent プロパティから、多くの他の API メンバーが名前を取得している、例えば GetChildren()FindFirstChild() など。このプロパティは、オブジェクトがエクスペリエンスに存在するか、削除する必要があるかを管理するのにも使用されます。オブジェクトの親が DataModel にある限り、変数に保存されたり、別のオブジェクトのプロパティに参照されたりして、オブジェクトは経験に残ります;そうでない場合、オブジェクトは自動的に削除されます。

呼び出し Destroy() は、ParentInstance とすべての子孫を nil に設定し、また ロック プロパティを Parent します。破壊されたオブジェクトの Parent を設定すると、エラーが発生します。

新しく作成されたオブジェクトは、Instance.new() を使用して作成され、通常、親が設定されるまで表示されたり機能したりしません。

オブジェクトレプリケーション

サーバーによって作成されたオブジェクトは、レプリケートされる対象に親属するまで、クライアントにレプリケートされません。オブジェクトを作成し、多くのプロパティを設定するときは、Parent プロパティを設定することをお勧めします。When creating an object and setting many properties, it's recommended to set the 最後 property last .これにより、複数のプロパティ変更をレプリケートするのではなく、オブジェクトが 1 回レプリケートされます。


local Workspace = game:GetService("Workspace")
-- 新しいインスタンスの親を最後に設定する (推奨)
local part = Instance.new("Part")
part.Position = Vector3.new(0, 10, 0)
part.Parent = Workspace

しかし、親がまだ設定されていない Model のパーツに親を付ける場合、そのモデルに各パーツを親に付けることは可能です。モデルは再複製されないので、モデルに親を付けることは受け入れられます。

RobloxLocked

非表示
プラグインのセキュリティ
並列読み取り

このプロパティは、CoreGui サービスのオブジェクトを無許可の方法でユーザーに変更されるのを防ぐために使用されていました。廃止されており、何もしません。

Sandboxed

複製されていません
並列読み取り

インスタンスを サンドボックス化コンテナ に変換し、特定のコンテナ内のスクリプトが実行できるアクションを制限する実験的な機能。詳細については、スクリプト機能 を参照してください。

UniqueId

UniqueId
複製されていません
スクリプト作成できません
Robloxのセキュリティ
並列読み取り

インスタンスのユニークな識別子で、必ずしもユニークではない Instance.Name とは異なります。

方法

AddTag

()

このメソッドは、タグをインスタンスに適用し、タグがすでに適用されている場合は効果がありません。タグを成功裏に追加すると、指定されたタグで CollectionService: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 に設定など) が良い考えです。これを行うには、Instance:RemoveTag()Instance:Destroy() 、または CollectionService:GetInstanceRemovedSignal() によって返されたシグナルに接続された関数で、呼び出してください。

パラメータ

tag: string
既定値: ""

戻り値

()

ClearAllChildren

()

この関数は、インスタンスの子供と孫すべてを破壊します。


local part = Instance.new("Part")
-- Add some sparkles
for i = 1, 3 do
local sparkles = Instance.new("Sparkles")
sparkles.Parent = part
local sc = Instance.new("Sparkles")
sc.Parent = sparkles
end
print("Children:", #part:GetChildren()) --> Children: 3
part:ClearAllChildren()
print("Children:", #part:GetChildren()) --> Children: 0

すべての子孫とその子孫を破壊したくない場合は、 すべて の子孫とその子孫をループし、破壊するものを選択するために Instance:GetChildren() または Instance:GetDescendants() を使用します。たとえば、次のコードサンプルは、 から下にすべてを破壊します:


local Workspace = game:GetService("Workspace")
local model = Workspace:FindFirstChild("TestModel")
for _, descendant in model:GetDescendants() do
if descendant:IsA("BasePart") then
descendant:Destroy()
end
end

戻り値

()

Clone() は、すべて Archivable でないインスタンスとすべての子孫のコピーを作成し、すべてのインスタンスを無視します。このメソッドでは、根インスタンスのコピーが返され、その Parentnil に設定されます。注: インスタンス自体が に設定されている場合、この関数は を返します。

参照プロパティ (例: ObjectValue.Value) がクローンインスタンスに設定された場合、コピーのプロパティの値はオリジナルの値に依存します:

  • 参照プロパティが クローンされたインスタンスを参照している場合、コピーはコピーに参照します。
  • 参照プロパティが クローンされなかったオブジェクトを参照している場合、コピーに同じ値が保持されます

戻り値

コードサンプル

Demonstrates cloning a model using Instance:Clone().

Cloning an Instance

local Workspace = game:GetService("Workspace")
-- Get a reference to an existing object
local model = script.Parent.Model
-- Create a clone of the model
local clone = model:Clone()
-- Move the clone so it's not overlapping the original model
clone:PivotTo(model.PrimaryPart.CFrame - (Vector3.xAxis * 10))
-- Add the clone to the Workspace
clone.Parent = Workspace

Destroy

()

Instance.Parent プロパティを nil に設定し、Instance.Parent プロパティをロックし、すべての接続を切断し、すべての子供に Destroy() を呼び出します。この機能は、もはや必要ないオブジェクトを処分する正しい方法です。

不要なオブジェクトの処分は重要であり、場所の不要なオブジェクトと接続は、時間経過により深刻なパフォーマンスの問題につながる可能性があるメモリを使用します。

オブジェクトを呼び出した後のベストプラクティスとして、オブジェクト (またはその子孫) を参照するすべての変数を に設定します。これにより、コードがオブジェクトに関連するものにアクセスできなくなります。


local part = Instance.new("Part")
part.Name = "Hello, world"
part:Destroy()
-- これはしないでください:
print(part.Name) --> "Hello, 世界 "
-- 上の行が機能しないようにするには、次の手順を実行してください:
part = nil

このメソッドで Instance が破壊されたら、Instance.Parent プロパティがロックされて再使用できなくなります。To 一時的に 破壊するのではなく、オブジェクトを削除せずに、Parentnil に設定します。例えば:


local Workspace = game:GetService("Workspace")
object.Parent = nil
task.wait(2)
object.Parent = Workspace

設定された時間後にオブジェクトを破壊するには、Debris:AddItem() を使用します。


戻り値

()

コードサンプル

Demonstrates destroying a Part using the Instance:Destroy() function.

This function is the correct way to dispose of objects that are no longer required.

Instance:Destroy()

local part = script.Parent.Part
part:Destroy()

FindFirstAncestor

並列書き込み

返す 最初の祖先の の が指定された名前と同じである の 最初の祖先。

この機能は上向きで、つまりインスタンスの直接的な Instance.Parent から始まり、DataModel に向かって作動します。一致する祖先が見つからない場合、nil を返します。

次のコードスニペットは、名前が Car のオブジェクトの最初の祖先を見つけます。


local car = object:FindFirstAncestor("Car")

特定のクラスの祖先を見つけるこの機能のバリアントについては、Instance:FindFirstAncestorOfClass() および Instance:FindFirstAncestorWhichIsA() を参照してください。

パラメータ

name: string

検索する Instance.Name

既定値: ""

戻り値

見つかった Instance

FindFirstAncestorOfClass

並列書き込み

返すのは、Instance の最初の祖先で、Object.ClassName が指定された classNameと同等のもの。

この機能は上向きで、つまりインスタンスの直接的な Instance.Parent から始まり、DataModel に向かって作動します。一致する祖先が見つからない場合、nil を返します。

この機能の一般的な使用は、Model に属する BasePart を見つけることです。例えば:


local model = part:FindFirstAncestorOfClass("Model")

この機能は、Instance:FindFirstAncestor() よりも Object.ClassName プロパティをチェックする Instance.Name の変種です。Instance:FindFirstAncestorWhichIsA() も存在し、クラスの継承を尊重するために Object:IsA() メソッドを代わりに使用しています。

パラメータ

className: string

検索する Object.ClassName

既定値: ""

戻り値

見つかった Instance

FindFirstAncestorWhichIsA

並列書き込み

返す 最初の祖先の Instance について、Object:IsA() が指定された className に対して true を返します。

この機能は上向きで、つまりインスタンスの直接的な Instance.Parent から始まり、DataModel に向かって作動します。一致する祖先が見つからない場合、nil を返します。

Instance:FindFirstAncestorOfClass() とは異なり、この関数はクラスの継承を尊重する Object:IsA() を使用します。たとえば:


print(part:IsA("Part")) --> true
print(part:IsA("BasePart")) --> true
print(part:IsA("Instance")) --> true

ただし、次のコードサンプルは、 、 、または であるかどうかにかかわらず、最初の祖先を返します。


local part = object:FindFirstAncestorWhichIsA("BasePart")

参照してください Instance:FindFirstAncestor()

パラメータ

className: string

検索する Object.ClassName

既定値: ""

戻り値

見つかった Instance

FindFirstChild

並列書き込み

返す Instance の最初の子供に、指定された名前を付け、または nil がそのような子供が存在しない場合。オプションの recursive 引数が true である場合、この関数は Instance の直接の子供だけでなく、すべての子孫を検索します。

オブジェクトの存在をチェックする

FindFirstChild() は、続行する前にオブジェクトが存在するかどうかを確認する必要がある場合に必要です。ドット演算子を使用して名前で子をインデックスに追加しようとすると、子が存在しない場合エラーが発生します。


local Workspace = game:GetService("Workspace")
-- パーツがワークスペースに存在しない場合の次の行エラー
Workspace.Part.Transparency = 0.5

より良いアプローチは、FindFirstChild() を最初に Part をチェックし、その後 if 文を使用して必要なコードを実行することです。


local Workspace = game:GetService("Workspace")
local part = Workspace:FindFirstChild("Part")
if part then
part.Transparency = 0.5
end
名前がプロパティと一致する子を見つける

時々、オブジェクトの Name は、その Parent のプロパティと同じです。ドット演算子を使用すると、名前を共有している場合、プロパティが子供より優先されます。

次の例では、Folder と呼ばれる ColorPart に追加され、これにも Part.Color プロパティが含まれています。注意してください part.Color は、子 Folder インスタンスではなく、Color3 プロパティ値を参照しています。FindFirstChild() を使用するメリットは、新しいプロパティの導入がコードにリスクを課すことがないということです。


local part = Instance.new("Part")
local folder = Instance.new("Folder")
folder.Name = "Color"
folder.Parent = part
local c1 = part.Color -- プロパティ
local c2 = part:FindFirstChild("Color") -- The child folder
性能の注意事項

FindFirstChild() はドット演算子を使用するより約 20% 長く、オブジェクトへの参照を保存するよりもほぼ 8 倍長くかかります。そのため、緊密なループまたは RunService.Heartbeat および RunService.PreRender に接続された関数などのパフォーマンスに依存するコードで呼び出すべきではありません。代わりに、結果を変数に保存するか、ChildAdded または WaitForChild() を使用して、指定された名前の子が利用可能になったときを検出する方法を検討します。

パラメータ

name: string

検索する Instance.Name

既定値: ""
recursive: boolean

検索が再帰的に実行されるかどうか。

既定値: false

戻り値

見つかった Instance

コードサンプル

以下は、ワークスペースで「ブリック」というオブジェクト名を検索します。見つかった場合、オブジェクト名を「フー」に変更します。

インスタンス:FindFirstChild

local found = workspace:FindFirstChild("Brick")
if found then
found.Name = "Foo"
end

FindFirstChildOfClass

並列書き込み

返す最初の子供の の が指定された と同じである 最初の子供の 。Instance:FindFirstChildWhichIsA() とは異なり、この関数はクラスの相続を無視して、クラスの属性が className に一致するオブジェクトのみを返します。一致する子供が見つからない場合、この関数は nil を返します。

パラメータ

className: string

検索する Object.ClassName

既定値: ""

戻り値

見つかった Instance

コードサンプル

Instance:FindFirstChildOfClass

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid
while not humanoid do
humanoid = character:FindFirstChildOfClass("Humanoid")
if not humanoid then
character.ChildAdded:Wait()
end
end

FindFirstChildWhichIsA

並列書き込み

Instance に対する最初の子供を返し、Object:IsA() が指定された className に対して true を返します。

一致する子が見つからない場合、この関数は nil を返します。オプションの再帰的な引数が true の場合、この関数は Instance の直接の子供だけでなく、すべての子孫を検索します。

Instance:FindFirstChildOfClass() とは異なり、この関数はクラスの継承を尊重する Object:IsA() を使用します。たとえば:


print(part:IsA("Part")) --> 真
print(part:IsA("BasePart")) --> 真
print(part:IsA("Instance")) --> true

そのため、次のコードサンプルは、 、 、または であるかどうかにかかわらず、最初の子供を返します。


local part = object:FindFirstChildWhichIsA("BasePart")

名前で子を検索している開発者は、代わりに Instance:FindFirstChild() を使用する必要があります。

パラメータ

className: string

検索する Object.ClassName

既定値: ""
recursive: boolean

検索が再帰的に実行されるかどうか。

既定値: false

戻り値

見つかった Instance

FindFirstDescendant

並列書き込み

指定された Instance.Name で見つかった最初の子孫を返します。

このメソッドは無効であり、使用できません。インスタンスの最初の子孫を見つけるには、代わりに recursive パラメータを Instance:FindFirstChild() に使用することを検討してください。

パラメータ

name: string

検索する Instance.Name

既定値: ""

戻り値

見つかった Instance

GetActor

並列書き込み

If the InstanceActor である場合、Actor 自体が返されます。そうでない場合、最も近い祖先 Actor が返されます。祖先が Actor でない場合、結果は nil です。


戻り値

見つかった Actor

GetAttribute

Variant
並列書き込み

このメソッドは、指定された属性名に割り当てられた値を返します。属性が割り当てられていない場合は、nil が返されます。

たとえば、次のコードスニペットは設定し、インスタンスの InitialPosition 属性の値を取得します:


local Workspace = game:GetService("Workspace")
local part = Workspace.Part
part:SetAttribute("InitialPosition", part.Position)
local initialPosition = instance:GetAttribute("InitialPosition")
print(initialPosition)
参照してください: See Also

パラメータ

attribute: string

回収される属性の名前。

既定値: ""

戻り値

Variant

指定された属性名に割り当てられた値。属性が割り当てられていない場合は、nil が返されます。

GetAttributeChangedSignal

この機能は、特定の指定された属性が変更されるときにのみ発動するなど、Changed イベントと同じように動作するイベントを返しますが、属性に関しては効果的に GetPropertyChangedSignal() と同じです。

一般的に、このメソッドを使用して、属性名をチェックする機能で Changed に接続するのではなく、このメソッドを使用するのが良いでしょう。同じ属性名を持つ同じオブジェクトでこのメソッドに後続の呼び出しは、同じイベントを返します。

次のコード例は、パーツの InitialPosition 属性が変更されたときに機能を発動するシグナルを返します attributeChanged()


local Workspace = game:GetService("Workspace")
local part = Workspace.Part
part:SetAttribute("InitialPosition", part.Position)
local function attributeChanged()
print("Attribute changed")
end
part:GetAttributeChangedSignal("InitialPosition"):Connect(attributeChanged)

インスタンス上で変更される属性がいつでも発射する Instance.AttributeChanged も参照してください。

パラメータ

attribute: string

変更シグナルが返される指定された属性の名前。

既定値: ""

戻り値

指定された属性が変更されたときに発動するイベント。

GetAttributes

並列書き込み

このメソッドは、キーが属性名で、値が非 nil 値の各属性に対するキー-バリューペアの辞書を返します。

たとえば、次のコードスニペットは、インスタンスの属性と値を出力します:


local Workspace = game:GetService("Workspace")
local part = Workspace.Part
part:SetAttribute("InitialPosition", part.Position)
part:SetAttribute("CanUse", true)
for name, value in part:GetAttributes() do
print(name .. " = " .. value)
end

また、Instance:GetAttribute() には、指定された属性名に割り当てられた値を返すものもあります。


戻り値

文字列が属性の名前であり、変体が非ゼロ値である各属性のストリング → 変体のペアの辞書。

GetChildren

Instances
並列書き込み

すべてのインスタンスの直接の子供を含む配列 (数字でインデックス化されたテーブル) または、オブジェクトと同じ Instance のすべての Parent を含む返すアレイを返します。配列は、数字または一般的な forループを使用して循環できます:


local Workspace = game:GetService("Workspace")
-- 数字のフォーループ例
local children = Workspace:GetChildren()
for i = 1, #children do
local child = children[i]
print(child.Name .. " is child number " .. i)
end

local Workspace = game:GetService("Workspace")
-- 一般的な forループの例
local children = Workspace:GetChildren()
for i, child in children do
print(child.Name .. " is child number " .. i)
end

子供は、Parent プロパティがオブジェクトに設定された順序でソートされます。

また、GetDescendants 機能も参照してください。


戻り値

Instances

インスタンスの子供を含む配列。

コードサンプル

The below would print the name of all objects currently in Workspace when ran.

Instance:GetChildren

local children = workspace:GetChildren()
for i = 1, #children do
print(i, children[i].Name)
end

GetDebugId

閲覧できません
プラグインのセキュリティ

Roblox 内で使用されているデバッグ ID のコード化された文字列を返します。注意:

  • このアイテムは保護されています。Script または LocalScript で使用しようとすると、エラーが発生します。
  • デバッグID は、デバッグプロセスで使用される IDです。デバッガーがアプリケーションが処理する前に、それぞれの指示を読むことができます。Roblox のすべてのオブジェクトはプロセスとして機能し、必要に応じてデバッグできる各実行指示(または「コード」)を実行します。
  • これは、同じ名前を共有するオブジェクトを区別する必要があるプラグインにとって有用である可能性があります (例: 同じ名前を共有するオブジェクト)。

パラメータ

scopeLength: number

スコープの長さ。

既定値: 4

戻り値

デバッグ ID ストリング。

コードサンプル

Instance:GetDebugId

print(workspace:GetDebugId()) --> 39FA_12
print(workspace:GetDebugId(10)) --> 39FA2FEF4D_12
print(workspace:GetDebugId(math.huge)) --> 12

GetDescendants

並列書き込み

このオブジェクトメソッドは、そのオブジェクトのすべての子孫を含む配列を返します。Instance:GetChildren() とは異なり、このメソッドはオブジェクトの即時の子供だけを返すのではなく、オブジェクトのすべての子供、それらの子供のすべての子供、などを見つけます。


戻り値

インスタンスの子孫を含む配列。

コードサンプル

GetDescendants is often used to do something to all the descendants that are a particular type of object. The code in this example uses GetDescendants and Instance:IsA() to find all of the parts in the workspace and turns them green.

Instance:GetDescendants

local descendants = workspace:GetDescendants()
-- Loop through all of the descendants of the Workspace. If a
-- BasePart is found, the code changes that parts color to green
for _, descendant in pairs(descendants) do
if descendant:IsA("BasePart") then
descendant.BrickColor = BrickColor.Green()
end
end

GetFullName

並列書き込み

インスタンスの祖先を説明する文字列を返します。文字列は、オブジェクトとその祖先の Name の連結で、期間で区切られています。The DataModel ( game ) は考慮されません。たとえば、Part 中の Workspace は、Workspace.Part を返す可能性があります。

When called on an Instance それは DataModel の子孫ではない場合、この関数は、Parent を含まないすべての祖先を考慮します。

この機能は、ログとデバッグに便利です。返された文字列を任意の有用な操作に解析しようとしてはならない; この関数はオブジェクト名に期間 (またはその他のシンボル) を脱出しない。言い換えれば、出力はしばしば有効な Luau 識別子のように見えますが、保証されていません。


戻り値

Instance のフル名。

コードサンプル

This code sample demonstrates the behavior of Instance:GetFullName(). It shows how the function behaves when called on an object not in the DataModel hierarchy, and it also shows how the return value does not escape special characters.

Instance:GetFullName

-- Create a simple hierarchy
local model = Instance.new("Model")
local part = Instance.new("Part")
part.Parent = model
local fire = Instance.new("Fire")
fire.Parent = part
print(fire:GetFullName()) --> Model.Part.Fire
model.Parent = workspace
print(fire:GetFullName()) --> Workspace.Model.Part.Fire
part.Name = "Hello, world"
print(fire:GetFullName()) --> Workspace.Model.Hello, world.Fire

This code sample re-implements the Instance:GetFullName() function in Lua.

Instance:GetFullName Lua Implementation

local function getFullName(object)
local result = object.Name
object = object.Parent
while object and object ~= game do
-- Prepend parent name
result = object.Name .. "." .. result
-- Go up the hierarchy
object = object.Parent
end
return result
end
print(getFullName(workspace.Camera)) --> Workspace.Camera

GetStyled

Variant

このメソッドは、指定されたプロパティのスタイル付きまたは明示的に変更された値を返すか、スタイル/修正されていない場合はデフォルトのプロパティ値を返します。これは、[GuiObject].Rotation のように、プロパティの値を直接アクセスするのとはわずかに異なり、プロパティのデフォルトまたは変更された値を返します。


local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local HUDContainer = playerGui:WaitForChild("HUDContainer")
local coreSheet = ReplicatedStorage:FindFirstChild("CoreSheet")
local rule = coreSheet:FindFirstChildWhichIsA("StyleRule")
rule.Selector = "TextButton"
-- ボタンへの参照
local button = HUDContainer:FindFirstChildWhichIsA("TextButton")
print(button:GetStyled("Rotation")) --> 0 (デフォルト値)
print(button.Rotation) --> 0 (デフォルト値)
-- スタイルルールプロパティを通じて回転を適用する
rule:SetProperty("Rotation", 30)
print(button:GetStyled("Rotation")) --> 30 (ルールに基づくスタイル付き値)
print(button.Rotation) --> 0 (デフォルト値)
-- スタイル付きプロパティを明示的に変更/オーバーライド
button.Rotation = 45
print(button:GetStyled("Rotation")) --> 45 (修正された値)
print(button.Rotation) --> 45 (modified value)

パラメータ

name: string

検索するプロパティの名前。

既定値: ""

戻り値

Variant

指定されたプロパティのスタイル付きまたは明示的に修正された値、またはスタイル/修正されていない場合はデフォルトのプロパティ値

GetStyledPropertyChangedSignal

このメソッドは、指定されたスタイルプロパティが変更されるときにのみ発動するのを除き、StyledPropertiesChanged イベントと同じように動作するイベントを返します。一般的に、このメソッドを使用して、プロパティ名をチェックする機能で StyledPropertiesChanged に接続するのではなく、このメソッドを使用するのが良い考えです。同じプロパティ名を持つ同じオブジェクトでこのメソッドに後続の呼び出しは、同じイベントを返します。

このイベントは接続された関数に引数を渡さないため、変更されたプロパティの値はスクリプト内で直接読み込まなければなりません。


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local HUDContainer = playerGui:WaitForChild("HUDContainer")
local meterBar = HUDContainer.MeterBar
local function stylePropertyChanged()
print("Style property changed!")
end
meterBar:GetStyledPropertyChangedSignal("AnchorPoint"):Connect(stylePropertyChanged)

パラメータ

property: string

変更を検出するスタイルプロパティの名前。

既定値: ""

戻り値

指定されたスタイルプロパティが変更されたときに発動するイベント。

GetTags

並列書き込み

このメソッドは、指定されたインスタンスに適用されたタグの配列を、文字列として返します。タグを追加できるのは、プロパティ ウィンドウで Studio で、またはランタイムで AddTag() です。

このメソッドは、インスタンスで一度に複数のタグを使用したいときに便利です。ただし、単一のタグの存在をチェックするためにこのメソッドを使用するのは非効率であり、代わりに HasTag() を使用して特定のタグをチェックします。


戻り値

HasTag

並列書き込み

このメソッドは、提供されたタグがオブジェクトに追加された場合に true を返します。タグを追加できるのは、プロパティ ウィンドウで Studio に、またはランタイムで AddTag() です。

パラメータ

tag: string
既定値: ""

戻り値

IsAncestorOf

並列書き込み

Instance が指定された後継者の祖先である場合、真を返します。

はオブジェクトの祖先と見なされ、オブジェクトの またはその親の が に設定されている場合、その親の祖先と見なされます。

参照してください, Instance:IsDescendantOf() .

パラメータ

descendant: Instance

子孫 Instance .

既定値: ""

戻り値

Instance が指定された後継者の祖先である場合は、真です。

コードサンプル

Demonstrates determining if one instance is the ancestor of another using Instance:IsAncestorOf()

Workspace and SpawnLocation are ancestors of the SpawnLocation's decal. Workspace is an ancestor of SpawnLocation.

SpawnLocation and its decal are descendants of Workspace, not ancenstors. Decal is a descendant to SpawnLocation, not an ancestor.

Instance:IsAncestorOf()

local Workspace = game:GetService("Workspace")
local spawnLocation = Workspace.SpawnLocation
local decal = spawnLocation.Decal
-- These statements are true
print(Workspace:IsAncestorOf(spawnLocation))
print(Workspace:IsAncestorOf(decal))
print(spawnLocation:IsAncestorOf(decal))
-- These statements are false
print(spawnLocation:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(Workspace))
print(decal:IsAncestorOf(spawnLocation))

IsDescendantOf

並列書き込み

true が指定された祖先の子孫である場合、Instance を返します。

注意: IsDescendantOf() は、オブジェクトが削除されたかどうかをチェックするために nil のパラメータを使用できません。

参照してください Instance:IsAncestorOf()

パラメータ

ancestor: Instance

祖先 Instance .

既定値: ""

戻り値

Instance が指定された祖先の子孫である場合は、真です。

コードサンプル

インスタンス:IsDescendantOf

local part = Instance.new("Part")
print(part:IsDescendantOf(game))
--> 偽
part.Parent = workspace
print(part:IsDescendantOf(game))
--> 真
part.Parent = game
print(part:IsDescendantOf(game))
--> true

IsPropertyModified

指定されたプロパティに保存された値がコードでインスタンス化されたデフォルトと同等の場合、true を返します。たとえば、出力の プロパティの が を指し示している場合、ラベルに を呼び出すと、ラベルが作成されるときに がデフォルト値になるため、スタジオの挿入ワークフローを介して挿入されるのではなく、ラベルを作成するときに が返されます。

このメソッドが true を返す場合、スタイリングは しません プロパティを明示的に変更することが優先されるため、スタイリングに影響しません。

パラメータ

property: string

検索するプロパティの名前。

既定値: ""

戻り値

プロパティが変更されているかどうかを示すブール。

RemoveTag

()

このメソッドは、インスタンスからタグを削除します。オブジェクトにタグがない場合、エラーをスローしません。タグを正常に削除すると、指定されたタグで CollectionService:GetInstanceRemovedSignal() によって作成されたシグナルが発射されます。

インスタンスをタグ付けするときは、タグの機能を提供するために、例えばイベント接続またはテーブルが使用されるのが一般的であることに注意してください。メモリ漏れを防ぐには、タグがもう必要ないときにこれらをクリーンアップする (接続を切断し、nil に設定など) が良い考えです。

パラメータ

tag: string
既定値: ""

戻り値

()

ResetPropertyToDefault

()

プロパティをデフォルト値にリセットします。たとえば、ResetPropertyToDefault("Rotation")TextLabel に呼び出すことは、その Rotation0 (プロパティのデフォルト値)に設定することと同等です。このメソッドを使用すると、スタイリングがこのプロパティのデフォルト値を上書きするように保証できます。

パラメータ

property: string

リセットするプロパティの名前。

既定値: ""

戻り値

()

SetAttribute

()

このメソッドは、指定された名前の属性を指定された値に設定します。値が nil である場合、アトリビュートは削除されます。デフォルトで nil が返されるためです。

たとえば、次のコードスニペットは、インスタンスの InitialPosition 属性を Vector3.new(0, 10, 0) に設定します:


local Workspace = game:GetService("Workspace")
local part = Workspace.Part
part:SetAttribute("InitialPosition", Vector3.new(0, 10, 0))
制限

命名要件と制限:

  • 名前はアルファ数字とアンダースコアのみを使用して야します。
  • スペースやユニークなシンボルは許可されません。
  • 文字列は 100 文字以下である必要があります。
  • 名前は、呼び出し元が Roblox コアスクリプトでない限り、 RBX で始まることは許可されていません。

アトリビュートを未対応のタイプに設定しようとすると、エラーが発生します。

参照してください: See Also

パラメータ

attribute: string

設定されている属性の名前。

既定値: ""
value: Variant

指定された属性を設定する値。

既定値: ""

戻り値

()

WaitForChild

イールド可能

返す Instance の子に指定された名前を付けます。子が存在しない場合、それが行われるまで現在のスレッドが返されます。timeOut パラメータが指定されると、このメソッドは指定された秒数後に期限切れになり、nil を返します。

主な使用法

WaitForChild() は、クライアントによって実行されるコードを LocalScript で作業するときに非常に重要です。Roblox エンジンは、オブジェクトがサーバーからクライアントにレプリケートされる時間または順序を保証しません。さらに、エクスペリエンスに Workspace.StreamingEnabled が真に設定されている場合、プレイヤーのキャラクターから遠く離れた BaseParts は、クライアントにストリームされない可能性があり、クライアント上にまだ存在しないオブジェクトをインデックス化すると、スクリプトが壊れる可能性があります。

ノート
  • この機能は、呼び出しが行われたときに、指定された名前の子が存在しない場合、返されません。
  • Instance:FindFirstChild() は、存在すると想定されるオブジェクトのための WaitForChild() よりも効率的な代替です。
  • このメソッドへの呼び出しが 5秒を超えて返さない場合、timeOut パラメータが指定されていない場合、スレッドが無期限に生成できる出力に警告が印刷されます。

パラメータ

childName: string

検索する Instance.Name

既定値: ""
timeOut: number

オプションの時間制限パラメータ。

既定値: ""

戻り値

見つかった Instance

コードサンプル

The following code waits for an instance named "Part" to be added to Workspace.

Instance:WaitForChild

local part = workspace:WaitForChild("Part")
print(part.Name .. " has been added to the Workspace")

イベント

AncestryChanged

オブジェクトの Instance.Parent プロパティまたは祖先の 1 つが変更されると、炎が発生します。

このイベントには、2つのパラメータが含まれています: child は、InstanceInstance.Parent が実際に変更されたものを参照し、parent は、このインスタンスの新しい Instance.Parent を参照します。

このイベントを使用して、Explorer での手動削除やプラグインを介した削除など、Studio でインスタンスの削除を追跡できます。インスタンスが Instance:Destroy() を使用して破壊されたときに検出する必要がある場合は、代わりに Instance.Destroying イベントを使用します。

パラメータ

child: Instance

変更された Instance の whose Instance.Parent は変更されました。

parent: Instance

その Instance.Parent の新しい InstanceInstance.Parent が変更されました。


コードサンプル

Demonstrates detecting changes to an instance's ancestry by connecting to the Instance.AncestryChanged event.

The ChangingPart's Parent is set to different values overtime. The parent of the part is the part's ancestor, so the Instance.AncestryChanged event will fire whenever it changes.

Instance.AncestryChanged

local Workspace = game:GetService("Workspace")
local redPart = script.Parent.RedPart
local bluePart = script.Parent.BluePart
local changingPart = script.Parent.ChangingPart
-- Change the color of changingPart based on it's Parent
local function onAncestryChanged(part: Part, parent: Instance)
if parent == redPart then
changingPart.Color = Color3.new(1, 0, 0)
elseif parent == bluePart then
changingPart.Color = Color3.new(0, 0, 1)
else
changingPart.Color = Color3.new(1, 1, 1)
end
print(`{part.Name} is now parented to {parent.Name}`)
end
changingPart.AncestryChanged:Connect(onAncestryChanged)
-- Set changingPart's Parent property to different instances over time
while true do
task.wait(2)
changingPart.Parent = redPart
task.wait(2)
changingPart.Parent = bluePart
task.wait(2)
changingPart.Parent = Workspace
end

AttributeChanged

このイベントは、アトリビュートが nil に設定されたときを含め、インスタンス上のアトリビュートが変更されるたびに発動します。変更された属性の名前が接続された関数に渡されます。

たとえば、次のコードスニペットは、パーツの属性の変更のいずれかが発生したときに attributeChanged() 関数を発射するように接続します:


local Workspace = game:GetService("Workspace")
local part = Workspace.Part
local function attributeChanged(attributeName)
print(attributeName, "changed")
end
part.AttributeChanged:Connect(attributeChanged)

特定の指定の属性が変更されたときに発動するイベントを返す Instance:GetAttributeChangedSignal() も参照してください。

パラメータ

attribute: string

変更された属性の名前。


ChildAdded

オブジェクトがこの Instance に親化された後に発火します。

この機能をクライアントで使用して、サーバーによって作成されたオブジェクトを検出するときは、これらのオブジェクトの子孫をインデックスするときに Instance:WaitForChild() を使用する必要があります。これは、オブジェクトとその子孫が同時にサーバーからクライアントに複製されることは保証されていないためです。例えば:


local Workspace = game:GetService("Workspace")
Workspace.ChildAdded:Connect(function(child)
-- Use WaitForChild() since descendants may not have replicated yet
local head = child:WaitForChild("Head")
end)

注: この機能は、Instance の直接の子供にのみ作動します。すべての子孫をキャプチャする機能を使用するには、Instance.DescendantAdded を使用します。

参照してください Instance.ChildRemoved

パラメータ

child: Instance

追加された Instance


コードサンプル

This snippet prints the names of objects as they are added to the Workspace:

Instance.ChildAdded

local function onChildAdded(instance)
print(instance.Name .. " added to the workspace")
end
workspace.ChildAdded:Connect(onChildAdded)
local part = Instance.new("Part")
part.Parent = workspace --> Part added to the Workspace

ChildRemoved

子供がこの Instance から削除された後に発火します。

削除は、オブジェクトの親がこの からこれ以外のものに変更されたときを指します。注: このイベントは、子供が破壊されたとき(Instance:Destroy() を使用して)、破壊機能がオブジェクトの親を nil に設定するときにも発動します。

この機能は、Instance の直接の子供にのみ作動します。すべての子孫をキャプチャする機能を使用するには、Instance.DescendantRemoving を使用します。

参照してください Instance.ChildAdded

パラメータ

child: Instance

削除された Instance


コードサンプル

This snippet prints the names of objects as they are removed from the Workspace:

Instance.ChildRemoved

local function onChildRemoved(instance)
print(instance.Name .. " removed from the workspace")
end
workspace.ChildRemoved:Connect(onChildRemoved)
local part = Instance.new("Part")
part.Parent = workspace
task.wait(2)
part:Destroy()

DescendantAdded

このイベントは、子孫が Instance に追加された後に発動します。

すべての子孫に発射すると、オブジェクトを親にすると、このオブジェクトとすべての子孫が個別にイベントを発射します。

If you're only concerned with the 直接の子供 of the Instance , use Instance.ChildAdded instead.

参照してください Instance.DescendantRemoving

パラメータ

descendant: Instance

追加された Instance


コードサンプル

This following example will print the name of any object that is added to the Workspace:

Instance.DescendantAdded

local function onDescendantAdded(descendant)
print(descendant)
end
workspace.DescendantAdded:Connect(onDescendantAdded)
local part = Instance.new("Part")
part.Parent = workspace

DescendantRemoving

このイベントは、親の 変更 が行われる直前に Instance 発動し、 後継者 インスタンスがもはや後継者ではなくなります。Destroy() は、インスタンスの Parentnil に変更し、親の子孫にこのメソッドを呼ぶと、このイベントが発動します。

このイベントは、子孫の削除の前に 発射されるため、このイベント発射時に親子孫の親は変更されません。子孫が親の直接の子供でもある場合、このイベントは より前に発動します。

子孫に子供がいる場合、このイベントはまず子孫が発射し、その子孫を続けます。

注意

このイベントは、削除されている子孫オブジェクトで発動します。子孫の Parent を別のものに設定しようとすると、失敗します。以下は、これを示す例です:


local Workspace = game:GetService("Workspace")
Workspace.DescendantRemoving:Connect(function(descendant)
-- Do not manipulate the parent of the descendant in this function!
-- This event fires BECAUSE the parent was manipulated, and the change hasn't happened yet
-- Therefore, it is problematic to change the parent like this:
descendant.Parent = game
end)
local part = Instance.new("Part")
part.Parent = Workspace
part.Parent = nil

参照してください DescendantAdded

パラメータ

descendant: Instance

削除されている Instance


コードサンプル

The following example prints the name of any descendant as it is being removed from the Workspace:

Instance.DescendantRemoving

workspace.DescendantRemoving:Connect(function(descendant)
print(descendant.Name .. " is currently parented to " .. tostring(descendant.Parent))
end)
local part = Instance.new("Part")
part.Parent = workspace
part.Parent = nil
--> Part is currently parented to Workspace
print(part.Parent)
--> nil

Destroying

接続された関数がまだ使用している間、Instance はメモリから削除されることはありません。しかし、機能がどこかで引き出された場合、Instance とその子孫はnil に親属することになります。

Workspace.SignalBehavior プロパティが Enum.SignalBehavior.Immediate に設定されている場合、このイベントは Instance またはその祖先の 1つが Instance:Destroy() で破壊される前にすぐに発動します。

プロパティが に設定されている場合、このイベントは次の再開ポイントで発動し、それは またはその祖先の 1つが で破壊される後です。

Deferred 行動では、スクリプトを自分のInstance.Destroyingイベントに接続することが問題であり、コールバックが呼び出される前にスクリプトが破壊されるため (つまり実行されない)。

Studio で Instance を削除するとき、手動で エクスプローラー を通じて削除するか、プラグインを通じて削除すると、Instance は破壊されません。代わりに、親は nil に設定され、Instance.AncestryChanged で追跡できます。


コードサンプル

This sample demonstrates how, when using Immediate signal behavior, an Instance being destroyed remains in place until the connected function yields.

Using the Destroying Event (Immediate signals)

local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("Before yielding:", part:GetFullName(), #part:GetChildren())
task.wait()
print("After yielding:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
part:Destroy()

This sample demonstrates how, when using Deferred signal behavior, an Instance is destroyed before the signal fires.

Using the Destroying Event (Deferred signals)

local part = Instance.new("Part", workspace)
local function onPartDestroying()
print("In signal:", part:GetFullName(), #part:GetChildren())
end
part.Destroying:Connect(onPartDestroying)
print("Before destroying:", part:GetFullName(), #part:GetChildren())
part:Destroy()
print("After destroying:", part:GetFullName(), #part:GetChildren())

StyledPropertiesChanged

このイベントは、プロパティが nil に設定されたときなど、インスタンス上のスタイルプロパティが変更されるたびに発動します。


local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local HUDContainer = playerGui:WaitForChild("HUDContainer")
local meterBar = HUDContainer.MeterBar
local function stylePropertyChanged()
print("Styled properties changed")
end
meterBar:StyledPropertiesChanged():Connect(stylePropertyChanged)