學習
引擎類別
ScriptDocument
無法建立
未複製

*此內容是使用 AI(Beta 測試版)翻譯,可能含有錯誤。若要以英文檢視此頁面,請按一下這裡


概要
方法
EditTextAsync(newText: string,startLine: number,startCharacter: number,endLine: number,endCharacter: number):Tuple
ForceSetSelectionAsync(cursorLine: number,cursorCharacter: number,anchorLine: number?,anchorCharacter: number?):Tuple
GetLine(lineIndex: number?):string
GetText(startLine: number?,startCharacter: number?,endLine: number?,endCharacter: number?):string
RequestSetSelectionAsync(cursorLine: number,cursorCharacter: number,anchorLine: number?,anchorCharacter: number?):Tuple
活動
SelectionChanged(positionLine: number,positionCharacter: number,anchorLine: number,anchorCharacter: number):RBXScriptSignal
繼承成員

API 參考
方法
CloseAsync
暫停
外掛程式安全性
ScriptDocument:CloseAsync():Tuple
返回
範例程式碼
腳本文件:關閉異步
--!nocheck
-- 在命令列中執行以下程式碼
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- 找到第一個打開的腳本文件
for _, document in documents do
-- 命令列不能關閉,因此不要選擇它
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`已關閉 {scriptDocument.Name}`)
else
warn(`無法 關閉 {scriptDocument.Name} 原因:{err}`)
end
else
print("沒有打開的 腳本")
end

EditTextAsync
暫停
外掛程式安全性
ScriptDocument:EditTextAsync(
newText:string, startLine:number, startCharacter:number, endLine:number, endCharacter:number
參數
newText:string
startLine:number
startCharacter:number
endLine:number
endCharacter:number
返回

ForceSetSelectionAsync
暫停
外掛程式安全性
ScriptDocument:ForceSetSelectionAsync(
cursorLine:number, cursorCharacter:number, anchorLine:number?, anchorCharacter:number?
參數
cursorLine:number
cursorCharacter:number
anchorLine:number?
預設值:"nil"
anchorCharacter:number?
預設值:"nil"
返回
範例程式碼
ScriptDocument:ForceSetSelectionAsync()
--!nocheck
-- 在腳本打開時在命令欄運行以下代碼
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 cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- 強制選擇整行文本
local success, err = scriptDocument:ForceSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("設置選擇!搞定")
else
print(`無法設置選擇,原因是: {err}`)
end
else
print("沒有打開的腳本")
end

GetLine
外掛程式安全性
ScriptDocument:GetLine(lineIndex:number?):string
參數
lineIndex:number?
預設值:"nil"
返回
範例程式碼
ScriptDocument.SelectionChanged 和 ScriptDocument:GetLine()
--!nocheck
-- 在腳本打開的情況下,在命令欄中運行以下代碼
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(`選取: 行 {positionLine}, 字元 {positionCharacter}`)
print(`錨點: 行 {anchorLine}, 字元 {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`選取的行文本: {lineText}`)
end)
else
print("沒有打開的腳本")
end

GetLineCount
外掛程式安全性
ScriptDocument:GetLineCount():number
返回
範例程式碼
ScriptDocument:獲取行數()
--!nocheck
-- 在命令欄執行以下程式碼,當一個腳本 打開時
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(`這個腳本有 {lineCount} 行!`)
else
print("沒有打開的腳本")
end

GetScript
外掛程式安全性
ScriptDocument:GetScript():LuaSourceContainer
範例程式碼
ScriptDocument:GetScript()
--!nocheck
-- 在 Command Bar 中運行以下代碼,當腳本打開時
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(`目前打開的腳本: {openScript:GetFullName()}`)
else
print("沒有打開的腳本")
end

GetSelectedText
外掛程式安全性
ScriptDocument:GetSelectedText():string
返回
範例程式碼
ScriptDocument:HasSelectedText() 和 :GetSelectedText()
--!nocheck
-- 在 Command Bar 中運行以下代碼,前提是有一個腳本打開
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(`當前選中的文本: {selectedText}`)
else
print("目前沒有選中的文本")
end
end)
else
print("沒有開啟的腳本")
end

GetSelection
外掛程式安全性
ScriptDocument:GetSelection():Tuple
返回

GetSelectionEnd
外掛程式安全性
ScriptDocument:GetSelectionEnd():Tuple
返回
範例程式碼
ScriptDocument:GetSelectionStart() 和 :GetSelectionEnd()
--!nocheck
-- 在有開啟腳本的情況下,在命令欄運行以下代碼
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(`選擇開始: 行 {startLine}, 字元 {startCharacter}`)
print(`選擇結束: 行 {endLine}, 字元 {endCharacter}`)
else
print("沒有開啟任何腳本")
end

GetSelectionStart
外掛程式安全性
ScriptDocument:GetSelectionStart():Tuple
返回
範例程式碼
ScriptDocument:GetSelectionStart() 和 :GetSelectionEnd()
--!nocheck
-- 在有開啟腳本的情況下,在命令欄運行以下代碼
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(`選擇開始: 行 {startLine}, 字元 {startCharacter}`)
print(`選擇結束: 行 {endLine}, 字元 {endCharacter}`)
else
print("沒有開啟任何腳本")
end

GetText
外掛程式安全性
ScriptDocument:GetText(
startLine:number?, startCharacter:number?, endLine:number?, endCharacter:number?
參數
startLine:number?
預設值:"nil"
startCharacter:number?
預設值:"nil"
endLine:number?
預設值:"nil"
endCharacter:number?
預設值:"nil"
返回
範例程式碼
腳本文件:獲取文本()
--!nocheck
-- 在腳本打開時,於命令欄中運行以下代碼
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(`腳本內容: {text}`)
else
print("沒有打開的腳本")
end

GetViewport
外掛程式安全性
ScriptDocument:GetViewport():Tuple
返回
範例程式碼
腳本文件:獲取視口
--!nocheck
-- 在命令列中運行以下代碼,當腳本 開啟時
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(`目前查看的行是 {firstLine}{lastLine}`)
else
print("沒有開啟的腳本")
end

HasSelectedText
外掛程式安全性
ScriptDocument:HasSelectedText():boolean
返回
範例程式碼
ScriptDocument:HasSelectedText() 和 :GetSelectedText()
--!nocheck
-- 在 Command Bar 中運行以下代碼,前提是有一個腳本打開
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(`當前選中的文本: {selectedText}`)
else
print("目前沒有選中的文本")
end
end)
else
print("沒有開啟的腳本")
end

IsCommandBar
外掛程式安全性
ScriptDocument:IsCommandBar():boolean
返回
範例程式碼
ScriptDocument:是命令列嗎()
--!nocheck
-- 在命令列中運行以下代碼
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if document:IsCommandBar() then
print("命令列文檔:", document)
end
end

MultiEditTextAsync
暫停
外掛程式安全性
ScriptDocument:MultiEditTextAsync(edits:{any}):Tuple
參數
edits:{any}
返回

RequestSetSelectionAsync
暫停
外掛程式安全性
ScriptDocument:RequestSetSelectionAsync(
cursorLine:number, cursorCharacter:number, anchorLine:number?, anchorCharacter:number?
參數
cursorLine:number
cursorCharacter:number
anchorLine:number?
預設值:"nil"
anchorCharacter:number?
預設值:"nil"
返回
範例程式碼
腳本文件:RequestSetSelectionAsync()
--!nocheck
-- 在有開啟的腳本時,在命令欄執行以下代碼
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 cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- 強制選擇整行文本
local success, err = scriptDocument:RequestSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("選擇已設置!")
else
print(`設置選擇失敗,原因:{err}`)
end
else
print("沒有開啟的腳本")
end

活動
SelectionChanged
外掛程式安全性
ScriptDocument.SelectionChanged(
positionLine:number, positionCharacter:number, anchorLine:number, anchorCharacter:number
參數
positionLine:number
positionCharacter:number
anchorLine:number
anchorCharacter:number
範例程式碼
ScriptDocument.SelectionChanged 和 ScriptDocument:GetLine()
--!nocheck
-- 在腳本打開的情況下,在命令欄中運行以下代碼
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(`選取: 行 {positionLine}, 字元 {positionCharacter}`)
print(`錨點: 行 {anchorLine}, 字元 {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`選取的行文本: {lineText}`)
end)
else
print("沒有打開的腳本")
end

ViewportChanged
外掛程式安全性
ScriptDocument.ViewportChanged(
startLine:number, endLine:number
參數
startLine:number
endLine:number
範例程式碼
連接到 ScriptDocument.ViewportChanged
--!nocheck
--[[
要運行:
1. 確保輸出視圖已開啟
2. 在命令欄中運行以下代碼
3. 在打開的腳本窗口中上下滾動
來自 ViewportChanged 事件的打印語句將出現在輸出中
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- 創建跨越多行的文本
local dummyText = string.rep("-- 假文本\n", 60)
-- 創建一個包含假文本的腳本並打開它
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`無法打開腳本,因為: {err}`)
return
end
-- 獲取對打開的腳本的引用
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`腳本視口已變更 - 開始行: {startLine}, 結束行: {endLine}`)
end
-- 連接視口變更事件到上面的函數,該函數打印更新後視口的開始和結束行
scriptDocument.ViewportChanged:Connect(onViewportChanged)

©2026 Roblox Corporation、Roblox、Roblox 標誌及 Powering Imagination 是我們在美國及其他國家地區的部分註冊與未註冊商標。