19 min read

Source: Roblox Creator Hub · CC BY 4.0 · View source · Code samples: MIT Imported 2026-10-03. Formatting adapted for this site.

ScriptDocument

Inherits from: Instance → Object

A ScriptDocument instance is a proxy of the document of a Studio Script Editor. It's different from the LuaSourceContainer open in the editor in that it represents the ephemeral state of an open document, and its representation is in a format that's more suited for reading and editing code than executing it. In particular, ScriptDocument reflects any changes that have been made to the open script in Drafts Mode, which the source property doesn't.

The Script Editor itself exists and changes on a different thread than any DataModel, so the ScriptDocument replicates the open Script Editor, but it isn't the open editor. Because of the replication, there's sometimes a slight delay between changing the text in the editor and updating the ScriptDocument. The delay usually occurs because the DataModel is busy, and it's almost always extremely small, but it still exists.

The existence of a ScriptDocument indicates that a document is open in the Script Editor. All ScriptDocument instances have ScriptEditorService as its parent. Each instance adheres to the following encoding conventions:

All APIs for ScriptDocument are at Plugin level security.

Inherits from: Instance

Memory category: Instances

Tags: NotCreatable, NotReplicated

Methods

NameType / ReturnsDescription
ScriptDocument:CloseAsyncTupleRequests that the editor associated with this document close. Yields the current thread until the editor responds to the request.
ScriptDocument:EditTextAsyncTupleReplaces the text in the specified range from (startLine, startColumn) to (endLine, endColumn) with newText.
ScriptDocument:ForceSetSelectionAsyncTupleAsks the editor to set its cursor selection to the argument values.
ScriptDocument:GetLinestringReturns the text of the specified line. When no argument is provided, returns the line of the current cursor position.
ScriptDocument:GetLineCountintReturns the number of lines in the document.
ScriptDocument:GetScriptLuaSourceContainerReturns the underlying LuaSourceContainer instance, if one exists, otherwise nil.
ScriptDocument:GetSelectedTextstringGets the text selected in the editor, or an empty string if there is no selection.
ScriptDocument:GetSelectionTupleReturns the last known selection of the Script Editor in the format: CursorLine, CursorChar, AnchorLine, AnchorChar. If the Script Editor has no selection, CursorLine == AnchorLine and CursorChar == AnchorChar.
ScriptDocument:GetSelectionEndTupleGets the larger of the cursor position and anchor. If the editor has no selection, they are the same value.
ScriptDocument:GetSelectionStartTupleGets the smaller of the cursor position and anchor. If the editor has no selection, they are the same value.
ScriptDocument:GetTextstringReturns text from the open editor.
ScriptDocument:GetViewportTupleReturns the currently displayed line numbers in the editor change.
ScriptDocument:HasSelectedTextbooleanReturns whether or not the editor has any text selected.
ScriptDocument:IsCommandBarbooleanReturns true if the ScriptDocument represents the Command bar.
ScriptDocument:MultiEditTextAsyncTupleApplies a batch of text edits to the document as a single atomic operation. Yields the current thread until the editor responds.
ScriptDocument:RequestSetSelectionAsyncTupleAsks the editor to set its cursor selection to the argument values.
ScriptDocument:ReviewableTextEditsAsyncTupleApplies a batch of line-based text edits that appear in the editor as reviewable inline diffs, allowing users to accept or reject each change.

Inherited from Instance

NameType / ReturnsDescription
Instance:AddTag()Applies a tag to the instance.
Instance:childrenInstancesReturns an array of the object's children.
Instance:ClearAllChildren()This method destroys all of an instance's children.
Instance:CloneInstanceCreate a copy of an instance and all its descendants, ignoring instances that are not Archivable.
Instance:cloneInstance
Instance:Destroy()Sets the Instance.Parent property to nil, locks the Instance.Parent property, disconnects all connections, and calls Destroy() on all children.
Instance:destroy()
Instance:FindFirstAncestorInstance?Returns the first ancestor of the Instance whose Instance.Name is equal to the given name.
Instance:FindFirstAncestorOfClassInstance?Returns the first ancestor of the Instance whose Object.ClassName is equal to the given className.
Instance:FindFirstAncestorWhichIsAInstance?Returns the first ancestor of the Instance for whom Object:IsA() returns true for the given className.
Instance:FindFirstChildInstance?Returns the first child of the Instance found with the given name.
Instance:findFirstChildInstance
Instance:FindFirstChildOfClassInstance?Returns the first child of the Instance whose ClassName is equal to the given class name.
Instance:FindFirstChildWhichIsAInstance?Returns the first child of the Instance for whom Object:IsA() returns true for the given className.
Instance:FindFirstDescendantInstance?Returns the first descendant found with the given Instance.Name.
Instance:GetActorActor?Returns the Actor associated with the Instance, if any.
Instance:GetAttributeVariantReturns the value which has been assigned to the given attribute name.
Instance:GetAttributeChangedSignalRBXScriptSignalReturns an event that fires when the given attribute changes.
Instance:GetAttributesDictionaryReturns a dictionary of the instance's attributes.
Instance:GetChildrenInstancesReturns an array containing all of the instance's children.
Instance:getChildrenInstances
Instance:GetDebugIdstringReturns a coded string of the debug ID used internally by Roblox.
Instance:GetDescendantsInstancesReturns an array containing all of the descendants of the instance.
Instance:GetFullNamestringReturns a string describing the instance's ancestry.
Instance:GetStyledVariantReturns the styled or explicitly modified value of the specified property, or else the default property value if it hasn't been styled/modified.
Instance:GetStyledPropertyChangedSignalRBXScriptSignalReturns an event that fires when the given style property changes on the instance.
Instance:GetTagsArrayGets an array of all tags applied to the instance.
Instance:HasTagbooleanCheck whether the instance has a given tag.
Instance:IsAncestorOfbooleanReturns true if an Instance is an ancestor of the given descendant.
Instance:IsDescendantOfbooleanReturns true if an Instance is a descendant of the given ancestor.
Instance:isDescendantOfboolean
Instance:IsPropertyModifiedbooleanReturns true if the value stored in the specified property is not equal to the code-instantiated default.
Instance:QueryDescendantsInstancesReturns an array containing all descendants of the instance that match the selector string.
Instance:Remove()Sets the object's Parent to nil, and does the same for all its descendants.
Instance:remove()
Instance:RemoveTag()Removes a tag from the instance.
Instance:ResetPropertyToDefault()Resets a property to its default value.
Instance:SetAttribute()Sets the attribute with the given name to the given value.
Instance:WaitForChildInstanceReturns the child of the Instance with the given name. If the child does not exist, it will yield the current thread until it does.

Inherited from Object

NameType / ReturnsDescription
Object:GetPropertyChangedSignalRBXScriptSignalGet an event that fires when a given property of the object changes.
Object:IsAbooleanReturns true if an object's class matches or inherits from a given class.
Object:isAboolean

ScriptDocument:CloseAsync

Requests that the editor associated with this document close. Yields the current thread until the editor responds to the request. If the function succeeds, it returns (true, nil). If the function fails, it returns (false, string) as a description of the problem.

This function can't close the command bar.

Returns

TypeDescription
TupleA tuple where the first element is true if the editor closed successfully, or false followed by a string describing the failure reason.
FieldValue
tags["Yields"]
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-CloseAsync).

ScriptDocument:EditTextAsync

Replaces the text in the specified range from (startLine, startColumn) to (endLine, endColumn) with newText. If the range is empty, then the function inserts the text at (startLine, startColumn). If the text cursor is within the specified range, the cursor moves to the end position of the edit. Otherwise, the text cursor doesn't move. This function yields the current thread until it receives a reply from the editor about the edit.

If the function succeeds, it returns (true, nil).

The function throws an error if:

If the function fails, it returns (false, string). The string is a description of the problem. The most common failure type is a version mismatch. This occurs when you try to call EditTextAsync during the time when the ScriptDocument is out of sync with the contents of the editor. If this happens, you can retry the edit.

Parameters

NameTypeDefaultDescription
newTextstringThe replacement string to insert into the specified range.
startLineintThe 1-indexed line number where the replacement range begins.
startCharacterintThe 1-indexed UTF-8 byte offset within startLine where the replacement range begins.
endLineintThe 1-indexed line number where the replacement range ends (exclusive).
endCharacterintThe 1-indexed UTF-8 byte offset within endLine where the replacement range ends (exclusive).

Returns

TypeDescription
TupleA tuple where the first element is true if the edit was applied successfully, or false followed by a string describing the failure reason.
FieldValue
tags["Yields"]
securityPluginSecurity
thread safetyUnsafe

ScriptDocument:ForceSetSelectionAsync

Asks the editor to set its cursor selection to the argument values. Both anchor arguments must be passed, or neither. If neither is passed, then they each default to being the same as the corresponding cursor argument. The editor might decline to update its cursor if the text content of the document has changed. Unlike ScriptDocument:RequestSetSelectionAsync(), the editor will not decline to move its cursor if the cursor has moved since the request was made. Returns (true, nil) if the cursor was updated, and (false, string) with an explanation string if it was not. Yields the current thread until the editor replies.

Parameters

NameTypeDefaultDescription
cursorLineintThe 1-indexed line number for the cursor position.
cursorCharacterintThe 1-indexed UTF-8 byte offset within the line for the cursor position.
anchorLineint?nilThe 1-indexed line number for the anchor position. Defaults to cursorLine if not provided.
anchorCharacterint?nilThe 1-indexed UTF-8 byte offset within the line for the anchor position. Defaults to cursorCharacter if not provided.

Returns

TypeDescription
TupleA tuple where the first element is true if the cursor was updated, or false followed by a string explaining why the update was declined.
FieldValue
tags["Yields"]
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-ForceSetSelectionAsync).

ScriptDocument:GetLine

Returns the text of the specified line. When no argument is provided, returns the line of the current cursor position.

Parameters

NameTypeDefaultDescription
lineIndexint?nilThe 1-indexed line number to retrieve. Defaults to the current cursor line if not provided.

Returns

TypeDescription
stringThe text content of the specified line as a string.
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-SelectionChanged-GetLine).

ScriptDocument:GetLineCount

Returns the number of lines in the active document.

Returns

TypeDescription
intThe total number of lines in the document.
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-GetLineCount).

ScriptDocument:GetScript

Returns the underlying LuaSourceContainer instance, if one exists, otherwise nil.

Returns

TypeDescription
LuaSourceContainerThe LuaSourceContainer that the document is editing, or nil if the document does not represent a script instance in the place (e.g. the Command Bar is not a real script instance).
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-GetScript).

ScriptDocument:GetSelectedText

Gets the text selected in the editor, or an empty string if there is no selection.

Returns

TypeDescription
stringThe currently selected text as a string, or an empty string if nothing is selected.
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-HasSelectedText-GetSelectedText).

ScriptDocument:GetSelection

Returns the last known selection of the Script Editor in the format: CursorLine, CursorChar, AnchorLine, AnchorChar. If the Script Editor has no selection, CursorLine == AnchorLine and CursorChar == AnchorChar.

Returns

TypeDescription
TupleCursorLine, CursorChar, AnchorLine, AnchorChar.
FieldValue
securityPluginSecurity
thread safetyUnsafe

ScriptDocument:GetSelectionEnd

Gets the larger of the cursor position and anchor. If the editor has no selection, they are the same value.

Returns

TypeDescription
TupleA tuple of (line, character) representing the larger of the cursor and anchor positions, both 1-indexed.
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-GetSelectionStart-GetSelectionEnd).

ScriptDocument:GetSelectionStart

Gets the smaller of the cursor position and anchor. If the editor has no selection, they are the same value.

Returns

TypeDescription
TupleA tuple of (line, character) representing the smaller of the cursor and anchor positions, both 1-indexed.
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-GetSelectionStart-GetSelectionEnd).

ScriptDocument:GetText

Returns text from the open editor. Must be called with 0, 2 or 4 arguments:

Parameters

NameTypeDefaultDescription
startLineint?nilThe 1-indexed line number where the text range begins. Optional; omit all arguments to get the entire document.
startCharacterint?nilThe 1-indexed UTF-8 byte offset within startLine where the text range begins.
endLineint?nilThe 1-indexed line number where the text range ends (exclusive). Optional; omit to read from startLine/startCharacter to the end of the document.
endCharacterint?nilThe 1-indexed UTF-8 byte offset within endLine where the text range ends (exclusive).

Returns

TypeDescription
stringThe text content within the specified range as a string.
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-GetText).

ScriptDocument:GetViewport

Returns the currently displayed line numbers in the editor change. The editor displays the lines between startLine and endLine, inclusive. The first and last line might only display partially. For example, only the topmost pixel of the last line might be on screen. Furthermore, code folding might hide lines between startLine and endLine.

Returns

TypeDescription
TupleA tuple of (startLine, endLine) representing the 1-indexed range of lines currently visible in the editor.
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-GetViewport).

ScriptDocument:HasSelectedText

Returns whether or not the editor has any text selected.

Returns

TypeDescription
booleantrue if the editor has a non-empty text selection, false otherwise.
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-HasSelectedText-GetSelectedText).

ScriptDocument:IsCommandBar

Returns true if the ScriptDocument represents the Command bar. The command bar has special rules and limitations in this API:

Returns

TypeDescription
booleantrue if this document represents the Command bar, false otherwise.
FieldValue
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-IsCommandBar).

ScriptDocument:MultiEditTextAsync

Applies a batch of text edits to the document as a single atomic operation, then yields the current thread until it receives a reply from the editor. Each entry in the edits array is a table describing one replacement, with the same meaning as the arguments to ScriptDocument:EditTextAsync(), and the following fields:

The edits are applied together, so either all of them succeed or none of them are applied. Before applying any edit, the function validates every edit and throws an error if:

If the function succeeds, it returns (true, nil). If the function fails, it returns (false, string), where the string describes the problem. As with ScriptDocument:EditTextAsync(), the most common failure is a version mismatch, which occurs when the ScriptDocument is momentarily out of sync with the contents of the editor; if this happens, you can retry the edits.

Parameters

NameTypeDefaultDescription
editsArrayAn array of tables, each describing a text replacement with fields NewText, StartLine, StartCharacter, EndLine, and EndCharacter.

Returns

TypeDescription
TupleA tuple where the first element is true if all edits were applied successfully, or false followed by a string describing the failure reason.
FieldValue
tags["Yields"]
securityPluginSecurity
thread safetyUnsafe

ScriptDocument:RequestSetSelectionAsync

Asks the editor to set its cursor selection to the argument values. Both anchor arguments must be passed, or neither. If neither is passed, then they each default to being the same as the corresponding cursor argument. The editor might decline to update its cursor if the text content of the document has changed, or the cursor has moved since the request was made. Returns (true, nil) if the cursor was updated, and (false, string) with an explanation string if it was not. Yields the current thread until the editor replies.

Parameters

NameTypeDefaultDescription
cursorLineintThe 1-indexed line number for the cursor position.
cursorCharacterintThe 1-indexed UTF-8 byte offset within the line for the cursor position.
anchorLineint?nilThe 1-indexed line number for the anchor position. Defaults to cursorLine if not provided.
anchorCharacterint?nilThe 1-indexed UTF-8 byte offset within the line for the anchor position. Defaults to cursorCharacter if not provided.

Returns

TypeDescription
TupleA tuple where the first element is true if the cursor was updated, or false followed by a string explaining why the update was declined.
FieldValue
tags["Yields"]
securityPluginSecurity
thread safetyUnsafe

Code samples: View on Creator Hub (ScriptDocument-RequestSetSelectionAsync).

ScriptDocument:ReviewableTextEditsAsync

Applies a batch of line-based text edits to the document that are presented to the user as reviewable inline diffs in the Script Editor gutter. Each entry in the changes array is a table with the following fields:

The edits must not overlap or border each other; there must be at least one unchanged line between any two edits. Edits are applied atomically. The function throws an error if any entry has invalid fields. If the function fails due to a version mismatch or other editor-side issue, it returns (false, string).

This method cannot be used on the Command bar.

Parameters

NameTypeDefaultDescription
changesArrayAn array of tables, each with a Text field (the replacement string), a StartLine field (1-indexed line to insert or begin replacing at), and an optional EndLine field (1-indexed last line to replace, inclusive).

Returns

TypeDescription
TupleA tuple where the first element is true if the edits were applied successfully, or false followed by a string describing the failure reason.
FieldValue
tags["Yields"]
securityPluginSecurity
thread safetyUnsafe

Events

NameType / ReturnsDescription
ScriptDocument.SelectionChangedFires when the ScriptDocument changes, including immediately after a text change.
ScriptDocument.ViewportChangedFires when the displayed line numbers in the editor change.

Inherited from Instance

NameType / ReturnsDescription
Instance.AncestryChangedFires when the Instance.Parent property of this object or one of its ancestors is changed.
Instance.AttributeChangedFires whenever an attribute is changed on the Instance.
Instance.ChildAddedFires after an object is parented to this Instance.
Instance.childAdded
Instance.ChildRemovedFires after a child is removed from this Instance.
Instance.DescendantAddedFires after a descendant is added to the Instance.
Instance.DescendantRemovingFires immediately before a descendant of the Instance is removed.
Instance.DestroyingFires immediately before (or is deferred until after) the instance is destroyed via Instance:Destroy().
Instance.StyledPropertiesChangedFires whenever any style property is changed on the instance, including when a property is set to nil.

Inherited from Object

NameType / ReturnsDescription
Object.ChangedFires immediately after a property of the object changes, with some limitations.

ScriptDocument.SelectionChanged

Fires when the ScriptDocument changes, including immediately after a text change.

Parameters

NameTypeDefaultDescription
positionLineint64The 1-indexed line number of the cursor position after the change.
positionCharacterint64The 1-indexed UTF-8 byte offset of the cursor position after the change.
anchorLineint64The 1-indexed line number of the selection anchor after the change.
anchorCharacterint64The 1-indexed UTF-8 byte offset of the selection anchor after the change.
FieldValue
securityPluginSecurity

Code samples: View on Creator Hub (ScriptDocument-SelectionChanged-GetLine).

ScriptDocument.ViewportChanged

Fires when the displayed line numbers in the editor change. See ScriptDocument.GetViewport for details.

Parameters

NameTypeDefaultDescription
startLineint64The 1-indexed first line currently visible in the editor viewport.
endLineint64The 1-indexed last line currently visible in the editor viewport.
FieldValue
securityPluginSecurity

Code samples: View on Creator Hub (ScriptDocument-ViewportChanged).

Properties

Inherited from Instance

NameType / ReturnsDescription
Instance.ArchivablebooleanDetermines if an Instance and its descendants can be cloned using Instance:Clone(), and can be saved/published.
Instance.archivableboolean
Instance.CapabilitiesSecurityCapabilitiesThe set of capabilities allowed to be used for scripts inside this container.
Instance.IsInSandboxbooleanIndicates whether the instance is inside a sandboxed container.
Instance.NamestringA non-unique identifier of the Instance.
Instance.ParentInstanceDetermines the hierarchical parent of the Instance.
Instance.PredictionModePredictionModeReflects the client-side prediction mode applied to the instance under server-authoritative physics.
Instance.RobloxLockedbooleanA deprecated property that used to protect CoreGui objects.
Instance.SandboxedbooleanWhen enabled, the instance can only access abilities in its Capabilities list.
Instance.UniqueIdUniqueIdA unique identifier for the instance.

Inherited from Object

NameType / ReturnsDescription
Object.ClassNamestringA read-only string representing the class this Object belongs to.
Object.classNamestring