ScriptDocument

Show Deprecated
Not Creatable
Not Replicated

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 Collaborative Editing, 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 text in ScriptDocument is UTF-8 encoded.
  • All line indices are 1-indexed.
  • All character indices are 1-indexed and count UTF-8 bytes, not graphemes, so the same warning from TextBox.CursorPosition applies: many Unicode characters take more than one byte.
  • All ranges are inclusive of their start position and exclusive of their end position, so start == end implies an empty range.

All APIs for ScriptDocument are at Plugin level security.

Summary

Properties

Events

SelectionChanged(positionLine: number, positionCharacter: number, anchorLine: number, anchorCharacter: number): RBXScriptSignal  

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

ViewportChanged(startLine: number, endLine: number): RBXScriptSignal  


Methods

GetLine(lineIndex: int?): string  

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


Returns the number of lines in the document.


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


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


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.


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


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

GetText(startLine: int?, startCharacter: int?, endLine: int?, endCharacter: int?): string  

Returns text from the open editor.


Returns whether or not the editor has any text selected.


Returns true if the ScriptDocument represents the Command bar.

CloseAsync(): Tuple  YIELDS


EditTextAsync(newText: string, startLine: number, startCharacter: number, endLine: number, endCharacter: number): Tuple  YIELDS

Replaces the text in the specified range from (startLine, startColumn) to (endLine, endColumn) with newText.

ForceSetSelectionAsync(cursorLine: number, cursorCharacter: number, anchorLine: int?, anchorCharacter: int?): Tuple  YIELDS

Asks the editor to set its cursor selection to the argument values.

RequestSetSelectionAsync(cursorLine: number, cursorCharacter: number, anchorLine: int?, anchorCharacter: int?): Tuple  YIELDS

Asks the editor to set its cursor selection to the argument values.

Properties

Events

SelectionChanged

Plugin Security

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

Parameters

positionLine: number
positionCharacter: number
anchorLine: number
anchorCharacter: number

Code Samples

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

1game:GetService("ScriptEditorService").TextDocumentDidOpen:Connect(
2 function(doc)
3 doc.SelectionChanged:Connect(
4 function()
5 print(doc:GetLine())
6 end)
7 end)
8

ViewportChanged

Plugin Security

Parameters

startLine: number
endLine: number

Methods

GetInternalUri

Roblox Script Security

Returns

GetLine

Plugin Security

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

Parameters

lineIndex: int?
Default Value: "nil"

Returns

Code Samples

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

1game:GetService("ScriptEditorService").TextDocumentDidOpen:Connect(
2 function(doc)
3 doc.SelectionChanged:Connect(
4 function()
5 print(doc:GetLine())
6 end)
7 end)
8

GetLineCount

Plugin Security

Returns the number of lines in the active document.


Returns

Code Samples

ScriptDocument:GetLineCount()

1game:GetService("ScriptEditorService").TextDocumentDidOpen:Connect(
2 function(doc)
3 print("Opened a doc with", doc:GetLineCount(), "lines!")
4 end)
5
Plugin Security

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


Code Samples

ScriptDocument:GetScript()

1game:GetService("ScriptEditorService").TextDocumentDidOpen:Connect(
2 function(doc)
3 print("Opened ", doc:GetScript())
4 end)
5

GetSelectedText

Plugin Security

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


Returns

Code Samples

ScriptDocument:HasSelectedText() and :GetSelectedText()

1local ScriptEditorService = game:GetService("ScriptEditorService")
2ScriptEditorService.TextDocumentDidOpen:Connect(function(Doc)
3 Doc.SelectionChanged:Connect(function()
4 if Doc:HasSelectedText() then
5 print("Your currently selected text is: ", Doc:GetSelectedText())
6 else
7 print("You have no text currently selected.")
8 end
9 end)
10end)
11

GetSelection

Plugin Security

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

CursorLine, CursorChar, AnchorLine, AnchorChar.

GetSelectionEnd

Plugin Security

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


Returns

Code Samples

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

1local ScriptEditorService = game:GetService("ScriptEditorService")
2local Document = ScriptEditorService:FindScriptDocument(workspace.Script)
3local sL, sC = Document:GetSelectionStart()
4local eL, eC = Document:GetSelectionEnd()
5Document:EditTextAsync("Replacement!", sL, sC, eL, eC)
6

GetSelectionStart

Plugin Security

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


Returns

Code Samples

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

1local ScriptEditorService = game:GetService("ScriptEditorService")
2local Document = ScriptEditorService:FindScriptDocument(workspace.Script)
3local sL, sC = Document:GetSelectionStart()
4local eL, eC = Document:GetSelectionEnd()
5Document:EditTextAsync("Replacement!", sL, sC, eL, eC)
6

GetText

Plugin Security

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

  • If called with 0 arguments, gets the entire contents of the open editor.
  • If called with 2 arguments, gets the text of the document starting at (startLine, startColumn).
  • If called with 4 arguments, gets the text of the document starting at (startLine, startColumn) and ending at (endLine, endColumn).

Parameters

startLine: int?
Default Value: "nil"
startCharacter: int?
Default Value: "nil"
endLine: int?
Default Value: "nil"
endCharacter: int?
Default Value: "nil"

Returns

Code Samples

ScriptDocument:GetText()

1game:GetService("ScriptEditorService").TextDocumentDidChange:Connect(
2 function(doc, _)
3 print("New Contents", doc:GetText())
4 end)
5

GetViewport

Plugin Security

Returns

HasSelectedText

Plugin Security

Returns whether or not the editor has any text selected.


Returns

Code Samples

ScriptDocument:HasSelectedText() and :GetSelectedText()

1local ScriptEditorService = game:GetService("ScriptEditorService")
2ScriptEditorService.TextDocumentDidOpen:Connect(function(Doc)
3 Doc.SelectionChanged:Connect(function()
4 if Doc:HasSelectedText() then
5 print("Your currently selected text is: ", Doc:GetSelectedText())
6 else
7 print("You have no text currently selected.")
8 end
9 end)
10end)
11

IsCommandBar

Plugin Security

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

  • Studio creates the the Command bar before running plugins, so it doesn't always fire the opened event, although it does close and reopen as Studio transitions between DataModels.
  • You can't edit the Command bar with EditTextAsync for security reasons.

Returns

Code Samples

ScriptDocument:IsCommandBar()

1for _, v in pairs(game:GetService("ScriptEditorService"):GetChildren()) do
2 if v:IsCommandBar() then
3 print(v)
4 end
5end
6

CloseAsync

Yields
Plugin Security

Returns

EditTextAsync

Yields
Plugin Security

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:

  • The range is invalid.
  • The range would slice a unicode character (e.g., replacing only some of the bytes of the unicode character).
  • The newText itself contains invalid UTF-8.

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

newText: string
startLine: number
startCharacter: number
endLine: number
endCharacter: number

Returns

ForceSetSelectionAsync

Yields
Plugin Security

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

cursorLine: number
cursorCharacter: number
anchorLine: int?
Default Value: "nil"
anchorCharacter: int?
Default Value: "nil"

Returns

Code Samples

ScriptDocument:ForceSetSelectionAsync()

1local ScriptEditorService = game:GetService("ScriptEditorService")
2local Document = ScriptEditorService:FindScriptDocument(workspace.Script)
3local Line = Document:GetSelection()
4local MatchStart, MatchEnd = Document:GetLine(Line):find("%d+")
5if MatchStart and MatchEnd then
6 Document:ForceSetSelectionAsync(Line, MatchStart, Line, MatchEnd + 1)
7end
8

RequestSetSelectionAsync

Yields
Plugin Security

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

cursorLine: number
cursorCharacter: number
anchorLine: int?
Default Value: "nil"
anchorCharacter: int?
Default Value: "nil"

Returns

Code Samples

ScriptDocument:RequestSetSelectionAsync()

1local ScriptEditorService = game:GetService("ScriptEditorService")
2local Document = ScriptEditorService:FindScriptDocument(workspace.Script)
3local Line = Document:GetSelection()
4local MatchStart, MatchEnd = Document:GetLine(Line):find("%d+")
5if MatchStart and MatchEnd then
6 Document:RequestSetSelectionAsync(Line, MatchStart, Line, MatchEnd + 1)
7end
8