ScriptDocument

Hiển Thị Bản Đã Lỗi Thời

*Nội dung này được dịch bằng AI (Beta) và có thể có lỗi. Để xem trang này bằng tiếng Anh, hãy nhấp vào đây.

Không Thể Tạo
Không Sao Chép

Một ví dụ ScriptDocument instance là một proxy của tài liệu của Trình soạn thảo Tập lệnh Studio.Nó khác với LuaSourceContainer mở trong trình soạn thảo ở chỗ nó đại diện cho trạng thái thoáng qua của một tài liệu mở, và sự thể hiện của nó là trong một định dạng phù hợp hơn để đọc và chỉnh sửa mã hơn là thực hiện nó.Cụ thể, ScriptDocument phản ánh bất kỳ thay đổi nào đã được thực hiện cho mã mở trong chế độ Bản nháp, mà thuộc tính nguồn không có.

Chính Trình soạn thảo Scrip tồn tại và thay đổi trên một luồng khác hơn bất kỳ DataModel , vì vậy ScriptDocument sao lưu Trình soạn thảo Scrip mở, nhưng nó không phải là trình soạn thảo mở.Vì replication, đôi khi có một chút trễ giữa việc thay đổi văn bản trong editor và cập nhật ScriptDocument.Thời gian trễ thường xảy ra vì DataModel bận rộn, và nó gần như luôn rất nhỏ, nhưng nó vẫn tồn tại.

Sự hiện hữu của một ScriptDocument cho thấy rằng một tài liệu đang mở trong Trình soạn thảo Tập lệnh.Tất cả các ScriptDocumentScriptEditorService làm cha của nó.Mỗi ví dụ tuân theo các tiêu chuẩn mã hóa sau:

  • Tất cả văn bản trong ScriptDocument được mã hóa bằng UTF-8.
  • Tất cả các chỉ mục dòng được đánh số 1.
  • Tất cả các chỉ mục nhân vật được đánh số 1 và đếm các bayt UTF-8, không phải là các grapheme, vì vậy cảnh báo tương tự từ TextBox.CursorPosition áp dụng: nhiều nhân vật Unicode cần hơn một bayt.
  • Tất cả các phạm vi bao gồm vị trí bắt đầu của chúng và loại trừ vị trí cuối cùng của chúng, vì vậy start == end có nghĩa là một phạm vi trống.

Tất cả các API cho ScriptDocument ở cấp độ bảo mật Plugin .

Tóm Tắt

Phương Pháp

  • GetLine(lineIndex : number?):string
    Bảo Mật Plugin

    Trả văn bản của dòng được chỉ định. Khi không cung cấp tham số, trả lại dòng của vị trí con trỏ hiện tại.

  • Bảo Mật Plugin

    Trả về số dòng trong tài liệu.

  • Bảo Mật Plugin

    Trả về ví dụ / trường hợpcơ sở LuaSourceContainer nếu có, nếu không có thì nil .

  • Bảo Mật Plugin

    Nhận văn bản được chọn trong trình soạn thảo, hoặc một chuỗi trống nếu không có lựa chọn.

  • Bảo Mật Plugin

    Trả về lựa chọn cuối cùng được biết đến của Trình biên tập Scrip trong định dạng: CursorLine, CursorChar, AnchorLine, AnchorChar . Nếu Trình biên tập Scrip không có lựa chọn, CursorLine == AnchorLineCursorChar == AnchorChar .

  • Bảo Mật Plugin

    Nhận vị trí con trỏ và neo lớn hơn. Nếu trình soạn thảo không có lựa chọn, chúng có giá trị tương tự.

  • Bảo Mật Plugin

    Nhận vị trí con trỏ và neo nhỏ hơn. Nếu trình soạn thảo không có lựa chọn, chúng có giá trị tương tự.

  • GetText(startLine : number?,startCharacter : number?,endLine : number?,endCharacter : number?):string
    Bảo Mật Plugin

    Trả văn bản từ trình soạn thảo mở.

  • Bảo Mật Plugin

    Trả về số dòng hiển thị hiện tại trong thay đổi biên tập.

  • Bảo Mật Plugin

    Trả về xem có hay không có văn bản được chọn bởi biên tập viên.

  • Bảo Mật Plugin

    Trả về true nếu ScriptDocument đại diện cho thanh Command.

  • Sinh Lợi
    Bảo Mật Plugin

    Các yêu cầu mà người biên tập liên kết với tài liệu này đóng. Tạo luồng hiện tại cho đến khi người biên tập trả lời yêu cầu.

  • EditTextAsync(newText : string,startLine : number,startCharacter : number,endLine : number,endCharacter : number):Tuple
    Sinh Lợi
    Bảo Mật Plugin

    Thay thế văn bản trong phạm vi được chỉ định từ ( startLine, startColumn ) đến ( endLine, endColumn ) với văn bản mới.

  • ForceSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    Sinh Lợi
    Bảo Mật Plugin

    Yêu cầu người soạn thảo đặt lựa chọn con trỏ của nó vào các giá trị tham số.

  • Sinh Lợi
    Bảo Mật Plugin
  • RequestSetSelectionAsync(cursorLine : number,cursorCharacter : number,anchorLine : number?,anchorCharacter : number?):Tuple
    Sinh Lợi
    Bảo Mật Plugin

    Yêu cầu người soạn thảo đặt lựa chọn con trỏ của nó vào các giá trị tham số.

Sự Kiện

Thuộc Tính

Phương Pháp

GetLine

Bảo Mật Plugin

Trả văn bản của dòng được chỉ định. Khi không cung cấp tham số, trả lại dòng của vị trí con trỏ hiện tại.

Tham Số

lineIndex: number
Giá Trị Mặc Định: "nil"

Lợi Nhuận

Mẫu mã

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Selected line text: {lineText}`)
end)
else
print("No scripts open")
end

GetLineCount

Bảo Mật Plugin

Trả về số dòng trong tài liệu hoạt động.


Lợi Nhuận

Mẫu mã

ScriptDocument:GetLineCount()

ScriptDocument:GetLineCount()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local lineCount = scriptDocument:GetLineCount()
print(`The script has {lineCount} lines!`)
else
print("No scripts open")
end
Bảo Mật Plugin

Trả về ví dụ / trường hợpcơ sở LuaSourceContainer nếu có, nếu không có thì nil .


Lợi Nhuận

Mẫu mã

ScriptDocument:GetScript()

ScriptDocument:GetScript()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local openScript = scriptDocument:GetScript()
print(`Currently open script: {openScript:GetFullName()}`)
else
print("No scripts open")
end

GetSelectedText

Bảo Mật Plugin

Nhận văn bản được chọn trong trình soạn thảo, hoặc một chuỗi trống nếu không có lựa chọn.


Lợi Nhuận

Mẫu mã

ScriptDocument:HasSelectedText() and :GetSelectedText()

ScriptDocument:HasSelectedText() and :GetSelectedText()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`Currently selected text: {selectedText}`)
else
print("No text currently selected")
end
end)
else
print("No scripts open")
end

GetSelection

Bảo Mật Plugin

Trả về lựa chọn cuối cùng được biết đến của Trình biên tập Scrip trong định dạng: CursorLine, CursorChar, AnchorLine, AnchorChar . Nếu Trình biên tập Scrip không có lựa chọn, CursorLine == AnchorLineCursorChar == AnchorChar .


Lợi Nhuận

CursorLine, CursorChar, Dòng neo, AnchorChar.

GetSelectionEnd

Bảo Mật Plugin

Nhận vị trí con trỏ và neo lớn hơn. Nếu trình soạn thảo không có lựa chọn, chúng có giá trị tương tự.


Lợi Nhuận

Mẫu mã

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`Selection start: Line {startLine}, Char {startCharacter}`)
print(`Selection end: Line {endLine}, Char {endCharacter}`)
else
print("No scripts open")
end

GetSelectionStart

Bảo Mật Plugin

Nhận vị trí con trỏ và neo nhỏ hơn. Nếu trình soạn thảo không có lựa chọn, chúng có giá trị tương tự.


Lợi Nhuận

Mẫu mã

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

ScriptDocument:GetSelectionStart() and :GetSelectionEnd()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local startLine, startCharacter = scriptDocument:GetSelectionStart()
local endLine, endCharacter = scriptDocument:GetSelectionEnd()
print(`Selection start: Line {startLine}, Char {startCharacter}`)
print(`Selection end: Line {endLine}, Char {endCharacter}`)
else
print("No scripts open")
end

GetText

Bảo Mật Plugin

Trả văn bản từ trình soạn thảo mở. Phải được gọi với 0, 2 hoặc 4 tham số:

  • Nếu được gọi với 0 tham số, nó nhận toàn bộ nội dung của editor mở.
  • Nếu được gọi với 2 tham số, nhận được văn bản của tài liệu bắt đầu tại ( startLine, startColumn ).
  • Nếu được gọi với 4 tham số, nó nhận được văn bản của tài liệu bắt đầu tại ( startLine , startColumn ) và kết thúc tại ( endLine , endColumn ).

Tham Số

startLine: number
Giá Trị Mặc Định: "nil"
startCharacter: number
Giá Trị Mặc Định: "nil"
endLine: number
Giá Trị Mặc Định: "nil"
endCharacter: number
Giá Trị Mặc Định: "nil"

Lợi Nhuận

Mẫu mã

ScriptDocument:GetText()

ScriptDocument:GetText()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local text = scriptDocument:GetText()
print(`Script contents: {text}`)
else
print("No scripts open")
end

GetViewport

Bảo Mật Plugin

Trả về số dòng hiển thị hiện tại trong thay đổi biên tập.Trình soạn thảo hiển thị các dòng giữa startLine và endLine, bao gồm.Dòng đầu tiên và cuối cùng có thể chỉ hiển thị một phần.Ví dụ, chỉ pixel trên cùng của dòng cuối cùng có thể ở trên màn hình.Ngoài ra, gấp mã có thể che đi các dòng giữa startLine và endLine.


Lợi Nhuận

Mẫu mã

ScriptDocument:GetViewport

ScriptDocument:GetViewport

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
local firstLine, lastLine = scriptDocument:GetViewport()
print(`Currently viewing lines {firstLine} to {lastLine}`)
else
print("No scripts open")
end

HasSelectedText

Bảo Mật Plugin

Trả về xem có hay không có văn bản được chọn bởi biên tập viên.


Lợi Nhuận

Mẫu mã

ScriptDocument:HasSelectedText() and :GetSelectedText()

ScriptDocument:HasSelectedText() and :GetSelectedText()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`Currently selected text: {selectedText}`)
else
print("No text currently selected")
end
end)
else
print("No scripts open")
end

IsCommandBar

Bảo Mật Plugin

Trả về true nếu ScriptDocument đại diện cho thanh lệnh. Thanh lệnh có các quy tắc và hạn chế đặc biệt trong API này:

  • Studio tạo thanh Command trước khi chạy plugin, vì vậy nó không phải lúc nào cũng kích hoạt sự kiện được mở, mặc dù nó đóng và mở lại khi Studio chuyển đổi giữa DataModels.
  • Bạn không thể chỉnh sửa thanh Command với EditTextAsync vì lý do an ninh.

Lợi Nhuận

Mẫu mã

ScriptDocument:IsCommandBar()

ScriptDocument:IsCommandBar()

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if document:IsCommandBar() then
print("Command bar document:", document)
end
end

CloseAsync

Sinh Lợi
Bảo Mật Plugin

Yêu cầu mà người biên tập liên kết với tài liệu này đóng.Sản xuất luồng hiện tại cho đến khi nhà biên tập trả lời yêu cầu.Nếu chức năng thành công, nó trả lại (true, nil).Nếu chức năng thất bại, nó trả về (false, chuỗi) như một mô tả về vấn đề.

Chức năng này không thể đóng thanh lệnh.


Lợi Nhuận

Mẫu mã

ScriptDocument:CloseAsync

ScriptDocument:CloseAsync

--!nocheck
-- Run the following code in the Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- Find the first open script document
for _, document in documents do
-- The Command Bar can't be closed, so don't select it
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`Closed {scriptDocument.Name}`)
else
warn(`Failed to close {scriptDocument.Name} because: {err}`)
end
else
print("No open scripts")
end

EditTextAsync

Sinh Lợi
Bảo Mật Plugin

Thay thế văn bản trong phạm vi được chỉ định từ ( startLine, startColumn ) đến ( endLine, endColumn ) với newText .Nếu phạm vi là trống, thì chức năng sẽ chèn văn bản tại ( startLine , startColumn ).Nếu con trỏ văn bản nằm trong phạm vi được định, con trỏ di chuyển đến vị trí cuối cùng của việc chỉnh sửa.Nếu không, con trỏ văn bản không di chuyển.Chức năng này trả về luồng hiện tại cho đến khi nó nhận được phản hồi từ người soạn thảo về việc chỉnh sửa.

Nếu chức năng thành công, nó trả lại ( true, nil ).

Chức năng ném lỗi nếu:

  • Phạm vi không hợp lệ.
  • Phạm vi sẽ cắt một ký tự Unicode, ví dụ chỉ thay thế một số bayt của ký tự Unicode.
  • Bản thân newText chứa UTF-8 không hợp lệ.

Nếu chức năng thất bại, nó trả về (false, chuỗi).Chuỗi là một mô tả về vấn đề.Loại lỗi phổ biến nhất là một trường hợp không phù hợp phiên bản.Điều này xảy ra khi bạn cố gọi EditTextAsync trong thời gian khi ScriptDocument bị mất đồng bộ với nội dung của editor.Nếu xảy ra, bạn có thể thử lại việc chỉnh sửa.

Tham Số

newText: string
Giá Trị Mặc Định: ""
startLine: number
Giá Trị Mặc Định: ""
startCharacter: number
Giá Trị Mặc Định: ""
endLine: number
Giá Trị Mặc Định: ""
endCharacter: number
Giá Trị Mặc Định: ""

Lợi Nhuận

ForceSetSelectionAsync

Sinh Lợi
Bảo Mật Plugin

Yêu cầu người soạn thảo đặt lựa chọn con trỏ của nó vào các giá trị tham số.Cả hai lập luận neo phải được truyền, hoặc không.Nếu không có được truyền, thì mỗi cái sẽ mặc định giống như các tham số con trỏ tương ứng.Người biên tập có thể từ chối cập nhật con trỏ nếu nội dung văn bản của tài liệu đã thay đổi.Không giống như ScriptDocument:RequestSetSelectionAsync(), trình soạn thảo sẽ không từ chối di chuyển con trỏ nếu con trỏ đã di chuyển kể từ khi yêu cầu được thực hiện.Trả về (đúng, nil) nếu con trỏ đã được cập nhật, và (giả mạo, chuỗi) với một chuỗi giải thích nếu nó không phải là vậy.Sản xuất luồng hiện tại cho đến khi nhà biên tập trả lời.

Tham Số

cursorLine: number
Giá Trị Mặc Định: ""
cursorCharacter: number
Giá Trị Mặc Định: ""
anchorLine: number
Giá Trị Mặc Định: "nil"
anchorCharacter: number
Giá Trị Mặc Định: "nil"

Lợi Nhuận

Mẫu mã

ScriptDocument:ForceSetSelectionAsync()

ScriptDocument:ForceSetSelectionAsync()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- Get the text on the cursor's current line
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Force select the entire line of text
local success, err = scriptDocument:ForceSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Set selection!")
else
print(`Failed to set selection because: {err}`)
end
else
print("No scripts open")
end

MultiEditTextAsync

Sinh Lợi
Bảo Mật Plugin

Tham Số

edits: Array
Giá Trị Mặc Định: ""

Lợi Nhuận

RequestSetSelectionAsync

Sinh Lợi
Bảo Mật Plugin

Yêu cầu người soạn thảo đặt lựa chọn con trỏ của nó vào các giá trị tham số.Cả hai lập luận neo phải được truyền, hoặc không.Nếu không có được truyền, thì mỗi cái sẽ mặc định giống như các tham số con trỏ tương ứng.Người biên tập có thể từ chối cập nhật con trỏ nếu nội dung văn bản của tài liệu đã thay đổi hoặc con trỏ đã di chuyển kể từ khi yêu cầu được đưa ra.Trả về (đúng, nil) nếu con trỏ đã được cập nhật, và (giả mạo, chuỗi) với một chuỗi giải thích nếu nó không phải là vậy.Sản xuất luồng hiện tại cho đến khi nhà biên tập trả lời.

Tham Số

cursorLine: number
Giá Trị Mặc Định: ""
cursorCharacter: number
Giá Trị Mặc Định: ""
anchorLine: number
Giá Trị Mặc Định: "nil"
anchorCharacter: number
Giá Trị Mặc Định: "nil"

Lợi Nhuận

Mẫu mã

ScriptDocument:RequestSetSelectionAsync()

ScriptDocument:RequestSetSelectionAsync()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
-- Get the text on the cursor's current line
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Force select the entire line of text
local success, err = scriptDocument:RequestSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Set selection!")
else
print(`Failed to set selection because: {err}`)
end
else
print("No scripts open")
end

Sự Kiện

SelectionChanged

Bảo Mật Plugin

Bắt lửa khi ScriptDocument thay đổi, bao gồm ngay sau khi thay đổi văn bản.

Tham Số

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

Mẫu mã

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

ScriptDocument.SelectionChanged and ScriptDocument:GetLine()

--!nocheck
-- Run the following code in the Command Bar while a script is open
local ScriptEditorService = game:GetService("ScriptEditorService")
local function getFirstOpenDocument()
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if not document:IsCommandBar() then
return document
end
end
return nil
end
local scriptDocument = getFirstOpenDocument()
if scriptDocument then
scriptDocument.SelectionChanged:Connect(function(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Selected line text: {lineText}`)
end)
else
print("No scripts open")
end

ViewportChanged

Bảo Mật Plugin

Bắt lửa khi số dòng hiển thị trong editor thay đổi. Xem ScriptDocument.GetViewport để biết chi tiết.

Tham Số

startLine: number
endLine: number

Mẫu mã

Demonstrates using ScriptDocument.ViewportChanged to print the start and end line of the script's viewport when it changes.

To run:

  1. Ensure Output view is open
  2. Run the below code in the Command Bar
  3. Scroll up and down in the opened Script window
Connecting to ScriptDocument.ViewportChanged

--!nocheck
--[[
To run:
1. Ensure Output view is open
2. Run the below code in the Command Bar
3. Scroll up and down in the opened Script window
Print statements from the ViewportChanged event will appear in the Output
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Create text that spans many lines
local dummyText = string.rep("-- Dummy Text\n", 60)
-- Create a script containing the dummy text and open it
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`Failed to open script because: {err}`)
return
end
-- Get a reference to the opened script
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`Script Viewport Changed - startLine: {startLine}, endLine: {endLine}`)
end
-- Connect the ViewportChanged event to the function above that prints the start and end line of the updated viewport
scriptDocument.ViewportChanged:Connect(onViewportChanged)