Skip to main content

Description

Inserts new lines into a script after a specified line number, without replacing any existing content.

Endpoint

insert_script_lines

Parameters

path
string
required
The path to the script instance in the DataModel tree
afterLine
number
required
The line number after which to insert the new lines (0-indexed, where 0 means insert at the beginning)
lines
string
required
The content to insert. 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 insertion

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

  • afterLine is 0-indexed: 0 means insert at the beginning, 1 means after the first line, etc.
  • The parameter is clamped to valid ranges (0 to total lines)
  • Existing lines are preserved and shifted down
  • Multiple lines can be inserted by including \n characters in the lines parameter
  • Does not replace any existing content

Examples

Insert at Beginning

local result = ScriptTools.insert_script_lines({
  path = "game.ServerScriptService.MainScript",
  afterLine = 0,
  lines = "-- New header comment\n-- Added at the top"
})

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

Insert After a Specific Line

local result = ScriptTools.insert_script_lines({
  path = "game.ServerScriptService.MainScript",
  afterLine = 5,
  lines = 'print("This is inserted after line 5")'
})

Insert Multiple Lines

local result = ScriptTools.insert_script_lines({
  path = "game.ServerScriptService.MainScript",
  afterLine = 10,
  lines = [[-- New function added
local function newFeature()
  print("New feature")
end]]
})

Insert at End

-- First, get the line count
local source = ScriptTools.get_script_source({
  path = "game.ServerScriptService.MainScript"
})

if source.lineCount then
  -- Insert after the last line
  local result = ScriptTools.insert_script_lines({
    path = "game.ServerScriptService.MainScript",
    afterLine = source.lineCount,
    lines = "\n-- Footer comment"
  })
end

Add a New Function

local result = ScriptTools.insert_script_lines({
  path = "game.ServerScriptService.MainScript",
  afterLine = 20,
  lines = [[
local function calculateDamage(base, multiplier)
  return base * multiplier
end
]]
})

Build docs developers (and LLMs) love