Lớp công cụ
ScriptDocument
*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.
Tóm Tắt
Phương Pháp
MultiEditTextAsync(edits: {any}):Tuple |
Sự Kiện
SelectionChanged(positionLine: number,positionCharacter: number,anchorLine: number,anchorCharacter: number):RBXScriptSignal |
ViewportChanged(startLine: number,endLine: number):RBXScriptSignal |
Tài Liệu Tham Khảo API
Phương Pháp
CloseAsync
Lợi Nhuận
Mẫu mã
ScriptDocument:CloseAsync
--!nocheck
-- Chạy mã sau trong Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
local scriptDocument
-- Tìm tài liệu script mở đầu tiên
for _, document in documents do
-- Command Bar không thể bị đóng, vì vậy không chọn nó
if not document:IsCommandBar() then
scriptDocument = document
break
end
end
if scriptDocument then
local success, err = scriptDocument:CloseAsync()
if success then
print(`Đã đóng {scriptDocument.Name}`)
else
warn(`Không thể đóng {scriptDocument.Name} vì: {err}`)
end
else
print("Không có script nào đang mở")
endEditTextAsync
ForceSetSelectionAsync
Lợi Nhuận
Mẫu mã
Tài liệu Script:ForceSetSelectionAsync()
--!nocheck
-- Chạy đoạn mã sau trong Command Bar trong khi một script đang mở
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
-- Lấy văn bản trên dòng hiện tại của con trỏ
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Buộc chọn toàn bộ dòng văn bản
local success, err = scriptDocument:ForceSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Đã thiết lập lựa chọn!")
else
print(`Không thể thiết lập lựa chọn vì: {err}`)
end
else
print("Không có script nào đang mở")
endGetLine
Tham Số
| Giá Trị Mặc Định: "nil" |
Lợi Nhuận
Mẫu mã
ScriptDocument.SelectionChanged và ScriptDocument:GetLine()
--!nocheck
-- Chạy mã sau trong Command Bar khi một script đang mở
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(`Đã chọn: Dòng {positionLine}, Ký tự {positionCharacter}`)
print(`Chốt: Dòng {anchorLine}, Ký tự {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Nội dung dòng đã chọn: {lineText}`)
end)
else
print("Không có script nào mở")
endGetLineCount
Lợi Nhuận
Mẫu mã
ScriptDocument:GetLineCount()
--!nocheck
-- Chạy đoạn mã sau trong Command Bar khi một script đang mở
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(`Script có {lineCount} dòng!`)
else
print("Không có script nào đang mở")
endGetScript
Lợi Nhuận
Mẫu mã
ScriptDocument:GetScript()
--!nocheck
-- Chạy mã sau trong Command Bar khi một script đang mở
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(`Script hiện đang mở: {openScript:GetFullName()}`)
else
print("Không có script nào đang mở")
endGetSelectedText
Lợi Nhuận
Mẫu mã
ScriptDocument:HasSelectedText() và :GetSelectedText()
--!nocheck
-- Chạy mã sau trong Command Bar khi một tập lệnh đang mở
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(`Văn bản hiện đang được chọn: {selectedText}`)
else
print("Không có văn bản nào được chọn")
end
end)
else
print("Không có tập lệnh nào đang mở")
endGetSelectionEnd
Lợi Nhuận
Mẫu mã
ScriptDocument:GetSelectionStart() và :GetSelectionEnd()
--!nocheck
-- Chạy mã sau trong Command Bar khi một script đang mở
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(`Điểm bắt đầu lựa chọn: Dòng {startLine}, Ký tự {startCharacter}`)
print(`Điểm kết thúc lựa chọn: Dòng {endLine}, Ký tự {endCharacter}`)
else
print("Không có script nào đang mở")
endGetSelectionStart
Lợi Nhuận
Mẫu mã
ScriptDocument:GetSelectionStart() và :GetSelectionEnd()
--!nocheck
-- Chạy mã sau trong Command Bar khi một script đang mở
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(`Điểm bắt đầu lựa chọn: Dòng {startLine}, Ký tự {startCharacter}`)
print(`Điểm kết thúc lựa chọn: Dòng {endLine}, Ký tự {endCharacter}`)
else
print("Không có script nào đang mở")
endGetText
Lợi Nhuận
Mẫu mã
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")
endGetViewport
Lợi Nhuận
Mẫu mã
ScriptDocument:GetViewport
---!nocheck
-- Chạy mã sau trong Command Bar khi một kịch bản đang mở
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(`Hiện tại đang xem các dòng {firstLine} đến {lastLine}`)
else
print("Không có kịch bản nào được mở")
endHasSelectedText
Lợi Nhuận
Mẫu mã
ScriptDocument:HasSelectedText() và :GetSelectedText()
--!nocheck
-- Chạy mã sau trong Command Bar khi một tập lệnh đang mở
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(`Văn bản hiện đang được chọn: {selectedText}`)
else
print("Không có văn bản nào được chọn")
end
end)
else
print("Không có tập lệnh nào đang mở")
endIsCommandBar
Lợi Nhuận
Mẫu mã
ScriptDocument:IsCommandBar()
--!nocheck
-- Chạy đoạn mã sau trong Command Bar
local ScriptEditorService = game:GetService("ScriptEditorService")
local documents = ScriptEditorService:GetScriptDocuments()
for _, document in documents do
if document:IsCommandBar() then
print("Tài liệu command bar:", document)
end
endMultiEditTextAsync
RequestSetSelectionAsync
Lợi Nhuận
Mẫu mã
Tài liệu kịch bản: Yêu cầu đặt lựa chọn bất đồng bộ()
--!nocheck
-- Chạy đoạn mã sau trong Thanh Lệnh khi một kịch bản đang mở
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
-- Lấy văn bản trên dòng hiện tại của con trỏ
local cursorLine = scriptDocument:GetSelection()
local lineText = scriptDocument:GetLine(cursorLine)
-- Buộc chọn toàn bộ dòng văn bản
local success, err = scriptDocument:RequestSetSelectionAsync(cursorLine, 1, cursorLine, #lineText + 1)
if success then
print("Đã đặt lựa chọn!")
else
print(`Không thể đặt lựa chọn vì: {err}`)
end
else
print("Không có kịch bản nào mở")
endSự Kiện
SelectionChanged
ScriptDocument.SelectionChanged(
Mẫu mã
ScriptDocument.SelectionChanged và ScriptDocument:GetLine()
--!nocheck
-- Chạy mã sau trong Command Bar khi một script đang mở
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(`Đã chọn: Dòng {positionLine}, Ký tự {positionCharacter}`)
print(`Chốt: Dòng {anchorLine}, Ký tự {anchorCharacter}`)
local lineText = scriptDocument:GetLine(positionLine)
print(`Nội dung dòng đã chọn: {lineText}`)
end)
else
print("Không có script nào mở")
endViewportChanged
Mẫu mã
Kết nối với ScriptDocument.ViewportChanged
--!nocheck
--[[
Để chạy:
1. Đảm bảo rằng khung Output đang mở
2. Chạy đoạn mã bên dưới trong Command Bar
3. Cuộn lên và xuống trong cửa sổ Script đã mở
Các câu lệnh Print từ sự kiện ViewportChanged sẽ xuất hiện trong Output
]]
local Workspace = game:GetService("Workspace")
local ScriptEditorService = game:GetService("ScriptEditorService")
-- Tạo văn bản trải dài nhiều dòng
local dummyText = string.rep("-- Dummy Text\n", 60)
-- Tạo một script chứa văn bản dummy và mở nó
local otherScript = Instance.new("Script")
otherScript.Source = dummyText
otherScript.Parent = Workspace
local success, err = ScriptEditorService:OpenScriptDocumentAsync(otherScript)
if not success then
warn(`Không thể mở script vì: {err}`)
return
end
-- Lấy tham chiếu đến script đã mở
local scriptDocument = ScriptEditorService:FindScriptDocument(otherScript)
local function onViewportChanged(startLine: number, endLine: number)
print(`Viewport Script Đã Thay Đổi - startLine: {startLine}, endLine: {endLine}`)
end
-- Kết nối sự kiện ViewportChanged với hàm ở trên để in ra dòng bắt đầu và kết thúc của viewport được cập nhật
scriptDocument.ViewportChanged:Connect(onViewportChanged)