Skip to main content

Description

Replaces a specified range of lines in a script with new content. The start and end lines are inclusive.

Endpoint

edit_script_lines

Parameters

path
string
required
The path to the script instance in the DataModel tree
startLine
number
required
The first line number to replace (1-indexed, inclusive)
endLine
number
required
The last line number to replace (1-indexed, inclusive)
replacement
string
required
The new content to insert in place of the specified lines. Can contain multiple lines separated by \n

Response

Success Response

ok
boolean
Returns true if the operation was successful
lineCount
number
The total number of lines in the script after the edit

Error Response

error
string
Error message if the operation failed. Possible errors:
  • Path resolution failure
  • Script read errors
  • Error from setting the Source property

Implementation Details

  • Line numbers are 1-indexed (first line is 1)
  • Start and end lines are clamped to valid ranges (1 to total lines)
  • The replacement can span multiple lines by including \n characters
  • The number of replacement lines doesn’t need to match the number of deleted lines
  • Lines outside the specified range are preserved

Examples

Replace a Single Line

local result = ScriptTools.edit_script_lines({
  path = "game.ServerScriptService.MainScript",
  startLine = 5,
  endLine = 5,
  replacement = 'print("Updated line")'
})

if result.ok then
  print("Edited successfully. New line count:", result.lineCount)
end

Replace Multiple Lines

local result = ScriptTools.edit_script_lines({
  path = "game.ServerScriptService.MainScript",
  startLine = 10,
  endLine = 15,
  replacement = [[local newFunction = function()
  print("This replaces lines 10-15")
end]]
})

Replace with Fewer Lines

-- Replace lines 20-25 (6 lines) with a single line
local result = ScriptTools.edit_script_lines({
  path = "game.ServerScriptService.MainScript",
  startLine = 20,
  endLine = 25,
  replacement = "-- Simplified code"
})

Replace with More Lines

-- Replace line 3 with multiple lines
local result = ScriptTools.edit_script_lines({
  path = "game.ServerScriptService.MainScript",
  startLine = 3,
  endLine = 3,
  replacement = [[-- New expanded code
local x = 1
local y = 2
local z = x + y]]
})

Build docs developers (and LLMs) love