Engine Class
ScriptDocument
*เนื้อหานี้แปลโดยใช้ AI (เวอร์ชัน Beta) และอาจมีข้อผิดพลาด หากต้องการดูหน้านี้เป็นภาษาอังกฤษ ให้คลิกที่นี่
สรุป
วิธีการ
MultiEditTextAsync(edits: {any}):Tuple |
เหตุการณ์
SelectionChanged(positionLine: number,positionCharacter: number,anchorLine: number,anchorCharacter: number):RBXScriptSignal |
ViewportChanged(startLine: number,endLine: number):RBXScriptSignal |
เอกสารอ้างอิงเกี่ยวกับ API
วิธีการ
CloseAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
เอกสารสคริปต์: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("ไม่มีสคริปต์ที่เปิดอยู่")
endEditTextAsync
ForceSetSelectionAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
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("ไม่มีสคริปต์เปิดอยู่")
endGetLine
พารามิเตอร์
| ค่าเริ่มต้น: "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("ไม่มีสคริปต์เปิดอยู่")
endGetLineCount
ส่งค่ากลับ
ตัวอย่างโค้ด
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("ไม่มีสคริปต์เปิดอยู่")
endGetScript
ส่งค่ากลับ
ตัวอย่างโค้ด
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")
endGetSelectedText
ส่งค่ากลับ
ตัวอย่างโค้ด
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("ไม่มีสคริปต์ที่เปิดอยู่")
endGetSelectionEnd
ส่งค่ากลับ
ตัวอย่างโค้ด
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("ไม่มีสคริปต์ที่เปิดอยู่")
endGetSelectionStart
ส่งค่ากลับ
ตัวอย่างโค้ด
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("ไม่มีสคริปต์ที่เปิดอยู่")
endGetText
ส่งค่ากลับ
ตัวอย่างโค้ด
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("ไม่มีสคริปต์เปิด")
endGetViewport
ส่งค่ากลับ
ตัวอย่างโค้ด
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("ไม่มีสคริปต์ที่เปิดอยู่")
endHasSelectedText
ส่งค่ากลับ
ตัวอย่างโค้ด
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("ไม่มีสคริปต์ที่เปิดอยู่")
endIsCommandBar
ส่งค่ากลับ
ตัวอย่างโค้ด
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
endMultiEditTextAsync
RequestSetSelectionAsync
ส่งค่ากลับ
ตัวอย่างโค้ด
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(
ตัวอย่างโค้ด
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("ไม่มีสคริปต์เปิดอยู่")
endViewportChanged
ตัวอย่างโค้ด
เชื่อมต่อกับ 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)