เรียนรู้
Engine Class
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
ส่งค่ากลับ
ตัวอย่างโค้ด
เอกสารสคริปต์:CloseAsync
--!nocheck
-- รันโค้ดต่อไปนี้ใน Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- ค้นหาเอกสารสคริปต์ที่เปิดอยู่แรก
for _, document in documents do
-- Command Bar ไม่สามารถปิดได้ ดังนั้นอย่าเลือกมัน
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
-- รันโค้ดต่อไปนี้ใน 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 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
-- รันโค้ดต่อไปนี้ใน 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(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`ข้อความบรรทัดที่เลือก: {lineText}`)
end)
else
print("ไม่มีสคริปต์เปิดอยู่")
end

GetLineCount
การรักษาความปลอดภัยของปลั๊กอิน
ScriptDocument:GetLineCount():number
ส่งค่ากลับ
ตัวอย่างโค้ด
ScriptDocument:GetLineCount()
--!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 lineCount = scriptDocument:GetLineCount()
print(`สคริปต์มี {lineCount} บรรทัด!`)
else
print("ไม่มีสคริปต์เปิดอยู่")
end

GetScript
การรักษาความปลอดภัยของปลั๊กอิน
ScriptDocument:GetScript():LuaSourceContainer
ส่งค่ากลับ
ตัวอย่างโค้ด
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
การรักษาความปลอดภัยของปลั๊กอิน
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
-- รันโค้ดต่อไปนี้ใน 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 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("ไม่มีสคริปต์ที่เปิดอยู่")
end

GetSelectionStart
การรักษาความปลอดภัยของปลั๊กอิน
ScriptDocument:GetSelectionStart():Tuple
ส่งค่ากลับ
ตัวอย่างโค้ด
ScriptDocument:GetSelectionStart() และ :GetSelectionEnd()
--!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 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("ไม่มีสคริปต์ที่เปิดอยู่")
end

GetText
การรักษาความปลอดภัยของปลั๊กอิน
ScriptDocument:GetText(
startLine:number?, startCharacter:number?, endLine:number?, endCharacter:number?
พารามิเตอร์
startLine:number?
ค่าเริ่มต้น: "nil"
startCharacter:number?
ค่าเริ่มต้น: "nil"
endLine:number?
ค่าเริ่มต้น: "nil"
endCharacter:number?
ค่าเริ่มต้น: "nil"
ส่งค่ากลับ
ตัวอย่างโค้ด
ScriptDocument:GetText()
--!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 text = scriptDocument:GetText()
print(`เนื้อหาสคริปต์: {text}`)
else
print("ไม่มีสคริปต์เปิด")
end

GetViewport
การรักษาความปลอดภัยของปลั๊กอิน
ScriptDocument:GetViewport():Tuple
ส่งค่ากลับ
ตัวอย่างโค้ด
ScriptDocument:GetViewport
--!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 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: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

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"
ส่งค่ากลับ
ตัวอย่างโค้ด
ScriptDocument:RequestSetSelectionAsync()
--!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 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
-- รันโค้ดต่อไปนี้ใน 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(positionLine, positionCharacter, anchorLine, anchorCharacter)
print(`Selected: Line {positionLine}, Char {positionCharacter}`)
print(`Anchor: Line {anchorLine}, Char {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. ตรวจสอบให้แน่ใจว่ามีการเปิดมุมมอง Output
2. รันโค้ดด้านล่างใน Command Bar
3. เลื่อนขึ้นและลงในหน้าต่าง Script ที่เปิดอยู่
ข้อความพิมพ์จากเหตุการณ์ ViewportChanged จะแสดงใน Output
]]
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(`Viewport ของสคริปต์เปลี่ยนแปลง - startLine: {startLine}, endLine: {endLine}`)
end
-- เชื่อมต่อเหตุการณ์ ViewportChanged กับฟังก์ชันด้านบนที่พิมพ์บรรทัดเริ่มต้นและบรรทัดสิ้นสุดของ viewport ที่อัปเดต
scriptDocument.ViewportChanged:Connect(onViewportChanged)

©2026 Roblox Corporation. Roblox, โลโก้ Roblox และ Powering Imagination เป็นส่วนหนึ่งของเครื่องหมายการค้าที่จดทะเบียน และไม่ได้จดทะเบียนของเราในสหรัฐฯ และประเทศอื่นๆ