学习
引擎类
ScriptDocument
无法创建
未复制

*此内容使用人工智能(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
返回
代码示例
脚本文档:CloseAsync
--!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"
返回
代码示例
脚本文档: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"
返回
代码示例
脚本文档.选定更改 和 脚本文档:获取行()
--!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
返回
代码示例
脚本文档:获取行数()
--!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
代码示例
脚本文档:获取脚本()
--!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 openScript = scriptDocument:GetScript()
print(`当前打开的脚本: {openScript:GetFullName()}`)
else
print("没有打开的脚本")
end

GetSelectedText
插件安全性
ScriptDocument:GetSelectedText():string
返回
代码示例
脚本文档:HasSelectedText() 和 :GetSelectedText()
--!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()
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
返回
代码示例
脚本文档:获取选择开始() 和 :获取选择结束()
--!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
返回
代码示例
脚本文档:获取选择开始() 和 :获取选择结束()
--!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
返回
代码示例
脚本文档:HasSelectedText() 和 :GetSelectedText()
--!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()
if scriptDocument:HasSelectedText() then
local selectedText = scriptDocument:GetSelectedText()
print(`当前选择的文本: {selectedText}`)
else
print("当前没有选择的文本")
end
end)
else
print("没有打开脚本")
end

IsCommandBar
插件安全性
ScriptDocument:IsCommandBar():boolean
返回
代码示例
脚本文档:IsCommandBar()
--!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"
返回
代码示例
脚本文档:请求选择异步()
--!不检查
-- 在脚本打开时在命令栏运行以下代码
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
代码示例
脚本文档.选定更改 和 脚本文档:获取行()
--!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("-- Dummy Text\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: {startLine}, endLine: {endLine}`)
end
-- 将 ViewportChanged 事件连接到上述函数,该函数打印更新的视口的起始行和结束行
scriptDocument.ViewportChanged:Connect(onViewportChanged)

©2026 Roblox Corporation、Roblox、Roblox 标志及 Powering Imagination 是我们在美国及其他国家或地区的注册与未注册商标。