Skip to main content

Overview

Insert new lines into a Roblox script at a specific position without rewriting the entire source. Perfect for adding new functions, variables, or code blocks.
Line numbers are 1-indexed. Use afterLine: 0 to insert at the very beginning of the script. Always use the numberedSource field from get_script_source to identify correct line numbers.

Parameters

instancePath
string
required
Roblox instance path to the script using dot notationExamples:
  • game.ServerScriptService.MainScript
  • game.StarterPlayer.StarterPlayerScripts.LocalScript
  • game.ReplicatedStorage.Modules.DataManager
afterLine
number
default:0
Insert after this line number (0-indexed for insertion position)
  • 0 = Insert at very beginning (before line 1)
  • 1 = Insert after line 1 (becomes new line 2)
  • 5 = Insert after line 5 (becomes new line 6)
Get from numberedSource field of get_script_source
newContent
string
required
Content to insert (can be multiple lines separated by \n)
  • Must include proper indentation
  • Must be valid Lua syntax
  • Each \n creates a new line

Response

{
  "success": true,
  "message": "3 lines inserted after line 25",
  "linesInserted": 3,
  "insertionPoint": 25,
  "totalLines": 158
}
success
boolean
Whether the operation completed successfully
message
string
Status message describing the operation
linesInserted
number
Number of lines added to the script
insertionPoint
number
The line number after which content was inserted
totalLines
number
Total lines in script after insertion

Usage Examples

Insert at Beginning (afterLine: 0)

Before:
1: local Players = game:GetService('Players')
2: 
3: print('Script started')
Request:
{
  "instancePath": "game.ServerScriptService.MainScript",
  "afterLine": 0,
  "newContent": "-- Main game script\n-- Version 2.0\n"
}
After:
1: -- Main game script
2: -- Version 2.0
3: 
4: local Players = game:GetService('Players')
5: 
6: print('Script started')

Insert New Function After Existing Code

Before:
10: local function playerJoined(player)
11:     print('Welcome', player.Name)
12: end
13: 
14: Players.PlayerAdded:Connect(playerJoined)
Request:
{
  "instancePath": "game.ServerScriptService.PlayerManager",
  "afterLine": 12,
  "newContent": "\nlocal function playerLeft(player)\n    print('Goodbye', player.Name)\nend"
}
After:
10: local function playerJoined(player)
11:     print('Welcome', player.Name)
12: end
13: 
14: local function playerLeft(player)
15:     print('Goodbye', player.Name)
16: end
17: 
18: Players.PlayerAdded:Connect(playerJoined)

Insert Variable Declarations

Before:
5: local Players = game:GetService('Players')
6: 
7: local function init()
Request:
{
  "instancePath": "game.ServerScriptService.Config",
  "afterLine": 5,
  "newContent": "local ReplicatedStorage = game:GetService('ReplicatedStorage')\nlocal ServerStorage = game:GetService('ServerStorage')"
}
After:
5: local Players = game:GetService('Players')
6: local ReplicatedStorage = game:GetService('ReplicatedStorage')
7: local ServerStorage = game:GetService('ServerStorage')
8: 
9: local function init()

Insert Code Inside Function

Before:
20: local function processData(data)
21:     -- Process the data
22:     return data
23: end
Request:
{
  "instancePath": "game.ServerScriptService.DataHandler",
  "afterLine": 21,
  "newContent": "    if not data then return nil end\n    data = sanitizeData(data)"
}
After:
20: local function processData(data)
21:     -- Process the data
22:     if not data then return nil end
23:     data = sanitizeData(data)
24:     return data
25: end

Best Practices

Step 1: Call get_script_source to see current structure
{"instancePath": "game.ServerScriptService.Script"}
Step 2: Identify insertion point from numberedSource:
10: local Players = game:GetService('Players')
11: 
12: local function init()
Step 3: Insert after the appropriate line:
{
  "instancePath": "game.ServerScriptService.Script",
  "afterLine": 10,
  "newContent": "local ReplicatedStorage = game:GetService('ReplicatedStorage')"
}
Ensure inserted code has correct indentation:
-- Root level: no indentation
local variable = true

function example()
    -- Inside function: 4 spaces
    if condition then
        -- Inside if: 8 spaces
        print('Hello')
    end
end
When inserting inside a function:
{
  "afterLine": 15,
  "newContent": "    print('Indented with 4 spaces')"
}
Include blank lines to maintain code structure:
Good: Includes spacing
{
  "afterLine": 20,
  "newContent": "\nlocal function newFunction()\n    return true\nend\n"
}
This creates:
21: 
22: local function newFunction()
23:     return true
24: end
25: 
Use \n to separate lines in the content:
{
  "afterLine": 10,
  "newContent": "local var1 = 1\nlocal var2 = 2\nlocal var3 = 3"
}
This inserts 3 lines after line 10.
  • afterLine: 0 → Insert before line 1 (at very beginning)
  • afterLine: 1 → Insert after line 1 (becomes new line 2)
Insert at beginning
{"afterLine": 0, "newContent": "-- Header comment"}
Insert after first line
{"afterLine": 1, "newContent": "-- After first line"}

Common Patterns

Add New Function at End

Get total line count first
{"instancePath": "game.ServerScriptService.Utils"}
// Response: {"lineCount": 50}
Insert after last line
{
  "instancePath": "game.ServerScriptService.Utils",
  "afterLine": 50,
  "newContent": "\nlocal function newHelper()\n    return 'new'\nend"
}

Add Service Imports at Top

Insert after initial comments
{
  "instancePath": "game.ServerScriptService.Main",
  "afterLine": 3,
  "newContent": "\nlocal HttpService = game:GetService('HttpService')\nlocal DataStoreService = game:GetService('DataStoreService')"
}

Add Debug Logging

Insert debugging code
{
  "instancePath": "game.ServerScriptService.GameLoop",
  "afterLine": 25,
  "newContent": "    print('DEBUG: Value is', value)\n    warn('DEBUG: Type is', typeof(value))"
}

Add Event Connection

Insert new event handler
{
  "instancePath": "game.ServerScriptService.Events",
  "afterLine": 40,
  "newContent": "\nRemoteEvent.OnServerEvent:Connect(function(player, data)\n    print('Received data from', player.Name)\nend)"
}

get_script_source

Read script with line numbers

set_script_source

Replace entire script

edit_script_lines

Replace specific line ranges

delete_script_lines

Delete line ranges

Error Handling

{
  "error": "afterLine exceeds script length",
  "afterLine": 150,
  "totalLines": 100
}

Complete Workflow Example

Step 1: Read to find insertion point
{
  "name": "get_script_source",
  "arguments": {
    "instancePath": "game.ServerScriptService.PlayerManager"
  }
}
Step 2: Analyze structure
15: -- Existing functions
16: local function onJoin(player)
17:     print('Joined')
18: end
19: 
20: -- Event connections
Step 3: Insert new function after line 18
{
  "name": "insert_script_lines",
  "arguments": {
    "instancePath": "game.ServerScriptService.PlayerManager",
    "afterLine": 18,
    "newContent": "\nlocal function onLeave(player)\n    print('Left')\nend"
  }
}
Step 4: Result
15: -- Existing functions
16: local function onJoin(player)
17:     print('Joined')
18: end
19: 
20: local function onLeave(player)
21:     print('Left')
22: end
23: 
24: -- Event connections

Build docs developers (and LLMs) love