# Instance（ロブロックス クラス）

> ロブロックス（Roblox）のすべての部品（パーツ・スクリプト・サービスなど）の元になるクラスです。Instance.new("Part") のようにして、新しい部品を作れます。

- URL：https://roblox.minecraft-okinawa.com/reference/classes/Instance/
- 公式：https://create.roblox.com/docs/reference/engine/classes/Instance
- 受け継いでいるクラス：Object

ゲームの中のものは、すべて「インスタンス」です。インスタンスは、親子の階層（エクスプローラーに表示される木の形）になって並んでいます。

Instanceのプロパティやメソッド（Name・Parent・Destroy・FindFirstChild・WaitForChildなど）は、どのクラスでも使える共通の機能です。

## 使用例：パーツを10個作って並べる

（Script／置く場所：ServerScriptService）

```lua
for i = 1, 10 do
	local part = Instance.new("Part")
	part.Size = Vector3.new(4, 1, 4)
	part.Position = Vector3.new(i * 5, 10, 0)
	part.Anchored = true
	part.BrickColor = BrickColor.random()
	part.Parent = workspace
end
```

## プロパティ

- `Instance.Archivable: boolean`：falseにすると、Cloneで複製されず、保存もされません。
- `Instance.Capabilities: SecurityCapabilities`：The set of capabilities allowed to be used for scripts inside this container.
- `Instance.IsInSandbox: boolean`：Indicates whether the instance is inside a sandboxed container.
- `Instance.Name: string`：インスタンスの名前。エクスプローラーに表示される名前で、スクリプトから探すときにも使います。
- `Instance.Parent: Instance`：親（ひとつ上の階層）のインスタンス。これを設定すると、そのインスタンスの中に入ります。nilにするとゲームから外れます。
- `Instance.PredictionMode: PredictionMode`：Reflects the client-side prediction mode applied to the instance under server-authoritative physics.
- `Instance.Sandboxed: boolean`：When enabled, the instance can only access abilities in its Capabilities list.

## メソッド

- `Instance:AddTag(tag: string): ()`：Applies a tag to the instance.
- `Instance:ClearAllChildren(): ()`：This method destroys all of an instance's children.
- `Instance:Clone(): Instance`：インスタンスを中身ごと複製します。複製したものは、Parentを設定するまでゲームに表示されません。
- `Instance:Destroy(): ()`：インスタンスを削除します。中に入っているものもまとめて消え、つながっているイベントも切れます。
- `Instance:FindFirstAncestor(name: string): Instance?`：指定した名前の祖先（親・親の親…）を探して返します。
- `Instance:FindFirstAncestorOfClass(className: string): Instance?`：Returns the first ancestor of the Instance whose Object.ClassName is equal to the given className.
- `Instance:FindFirstAncestorWhichIsA(className: string): Instance?`：Returns the first ancestor of the Instance for whom Class.Object:IsA() returns true for the given className.
- `Instance:FindFirstChild(name: string, recursive: boolean): Instance?`：指定した名前の子を探して返します。見つからないときはnilを返します。
- `Instance:FindFirstChildOfClass(className: string): Instance?`：Returns the first child of the Instance whose Class.Object.ClassName|ClassName is equal to the given class name.
- `Instance:FindFirstChildWhichIsA(className: string, recursive: boolean): Instance?`：指定したクラス（またはその仲間）の子を探して返します。
- `Instance:FindFirstDescendant(name: string): Instance?`：Returns the first descendant found with the given Instance.Name.
- `Instance:GetActor(): Actor?`：Returns the Actor associated with the Instance, if any.
- `Instance:GetAttribute(attribute: string): Variant`：インスタンスに付けた属性（アトリビュート）の値を取り出します。
- `Instance:GetAttributeChangedSignal(attribute: string): RBXScriptSignal`：Returns an event that fires when the given attribute changes.
- `Instance:GetAttributes(): Dictionary`：Returns a dictionary of the instance's attributes.
- `Instance:GetChildren(): Instances`：子をすべて配列（テーブル）で返します。
- `Instance:GetDebugId(scopeLength: int): string`：Returns a coded string of the debug ID used internally by Roblox.
- `Instance:GetDescendants(): Instances`：子・孫・ひ孫…をすべて配列で返します。
- `Instance:GetFullName(): string`：Returns a string describing the instance's ancestry.
- `Instance:GetStyled(name: string, selector: string?): Variant`：Returns the styled or explicitly modified value of the specified property, or else the default property value if it hasn't been styled/modified.
- `Instance:GetStyledPropertyChangedSignal(property: string): RBXScriptSignal`：Returns an event that fires when the given style property changes on the instance.
- `Instance:GetTags(): Array`：Gets an array of all tags applied to the instance.
- `Instance:HasTag(tag: string): boolean`：Check whether the instance has a given tag.
- `Instance:IsAncestorOf(descendant: Instance): boolean`：Returns true if an Instance is an ancestor of the given descendant.
- `Instance:IsDescendantOf(ancestor: Instance): boolean`：指定したインスタンスの中に入っているかを調べます。
- `Instance:IsPropertyModified(property: string): boolean`：Returns true if the value stored in the specified property is not equal to the code-instantiated default.
- `Instance:QueryDescendants(selector: string): Instances`：Returns an array containing all descendants of the instance that match the selector string.
- `Instance:RemoveTag(tag: string): ()`：Removes a tag from the instance.
- `Instance:ResetPropertyToDefault(property: string): ()`：Resets a property to its default value.
- `Instance:SetAttribute(attribute: string, value: Variant): ()`：インスタンスに属性（アトリビュート）を付けます。自分で決めた名前で値を保存できます。
- `Instance:WaitForChild(childName: string, timeOut: double): Instance`：指定した名前の子が現れるまで待ってから返します。LocalScriptで、まだ読み込まれていないものを使うときに必須です。

## イベント

- `Instance.AncestryChanged(child: Instance, parent: Instance)`：Fires when the Instance.Parent property of this object or one of its ancestors is changed.
- `Instance.AttributeChanged(attribute: string)`：Fires whenever an attribute is changed on the Instance.
- `Instance.ChildAdded(child: Instance)`：子が追加されたときに発生するイベントです。
- `Instance.ChildRemoved(child: Instance)`：子が取り除かれたときに発生するイベントです。
- `Instance.DescendantAdded(descendant: Instance)`：Fires after a descendant is added to the Instance.
- `Instance.DescendantRemoving(descendant: Instance)`：Fires immediately before a descendant of the Instance is removed.
- `Instance.Destroying()`：インスタンスが削除される直前に発生するイベントです。
- `Instance.StyledPropertiesChanged()`：Fires whenever any style property is changed on the instance, including when a property is set to nil.

英語の説明とメンバー一覧：Roblox Creator Documentation（CC BY 4.0）。日本語の解説はこのサイト独自のものです。

執筆：鈴木孝昌（現役プロジェクトマネージャー／プログラミング講師）／運営：沖縄マイクラ部 プログラミングスクール「クロスウェーブ」（https://minecraft-okinawa.com/）