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
Roblox instance path to the script using dot notationExamples:
game.ServerScriptService.MainScriptgame.StarterPlayer.StarterPlayerScripts.LocalScriptgame.ReplicatedStorage.Modules.DataManager
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)
numberedSource field of get_script_sourceContent to insert (can be multiple lines separated by
\n)- Must include proper indentation
- Must be valid Lua syntax
- Each
\ncreates a new line
Response
Whether the operation completed successfully
Status message describing the operation
Number of lines added to the script
The line number after which content was inserted
Total lines in script after insertion
Usage Examples
Insert at Beginning (afterLine: 0)
Before:Insert New Function After Existing Code
Before:Insert Variable Declarations
Before:Insert Code Inside Function
Before:Best Practices
Always Read Before Inserting
Always Read Before Inserting
Step 1: Call Step 2: Identify insertion point from Step 3: Insert after the appropriate line:
get_script_source to see current structurenumberedSource:Match Indentation of Surrounding Code
Match Indentation of Surrounding Code
Ensure inserted code has correct indentation:When inserting inside a function:
Add Blank Lines for Readability
Add Blank Lines for Readability
Include blank lines to maintain code structure:This creates:
Good: Includes spacing
Insert Multiple Lines with \\n
Insert Multiple Lines with \\n
Use This inserts 3 lines after line 10.
\n to separate lines in the content:Insert at Beginning vs After Line 1
Insert at Beginning vs After Line 1
afterLine: 0→ Insert before line 1 (at very beginning)afterLine: 1→ Insert after line 1 (becomes new line 2)
Insert at beginning
Insert after first line
Common Patterns
Add New Function at End
Get total line count first
Insert after last line
Add Service Imports at Top
Insert after initial comments
Add Debug Logging
Insert debugging code
Add Event Connection
Insert new event handler
Related Tools
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
Complete Workflow Example
Step 1: Read to find insertion point
Step 2: Analyze structure
Step 3: Insert new function after line 18
Step 4: Result