Instanceクラス

クラスInstance.newでは作れない日本語解説あり

ひとことで言うと

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

公式の説明(英語):Instance is the base class for all classes in the Roblox class hierarchy which can be part of the DataModel tree.

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

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

使用例

パーツを10個作って並べる
Script置く場所:ServerScriptService
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

スクリプトは、Luauの型チェック(luau-lsp)で確認しています。

クラスの関係

受け継いでいるクラス:Object → Instance

このクラスを受け継ぐクラス:AccessoryDescription、Accoutrement、AdPortal、AdService、AdvancedDragger、AnalyticsService、Animation、AnimationClip、AnimationClipProvider、AnimationController、AnimationNodeDefinition、AnimationRigData、AnimationTrack、AnimationValueNodeDefinition、AnimationValueOutputDefinition、Animator、AssetDeliveryProxy、AssetPatchSettings、AssetService、Atmosphere、AudioAnalyzer、AudioChannelMixer、AudioChannelSplitter、AudioChorus、AudioCompressor、AudioDeviceInput、AudioDeviceOutput、AudioDistortion、AudioEcho、AudioEmitter、AudioEqualizer、AudioFader、AudioFilter、AudioFlanger、AudioGate、AudioLimiter、AudioListener、AudioPitchShifter、AudioPlayer、AudioRecorder

プロパティ(7)

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

メソッド(31)

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

イベント(8)

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

受け継いでいるメンバー

Object から受け継いでいるもの(4)

ClassName・GetPropertyChangedSignal・IsA・Changed

Instanceを使う記事

英語の説明とメンバーの一覧は、Roblox Creator Documentation(Instance)をもとにしています(CC BY 4.0)。日本語の解説は、このサイトで独自に書いたものです。

執筆:鈴木孝昌官公庁のWeb・システム開発を担当する現役プロジェクトマネージャー。沖縄マイクラ部 クロスウェーブで、ロブロックス(Roblox)・マインクラフト(マイクラ)・Scratchなどのプログラミングを指導。マイクラカップ沖縄代表チームを指導(第6回・第7回、第7回TBS賞)。
沖縄マイクラ部で、子どもたちにロブロックス(Roblox)やマインクラフト(マイクラ)のプログラミングを教えています。プロフィールと編集方針
Instagramで教室の様子を見る
noteで記事を読む