Skip to main content

Overview

Replace the entire source code of a Roblox script. Uses ScriptEditorService:UpdateSourceAsync() to work seamlessly with open script editors in Roblox Studio.
This replaces the entire script. For partial edits, use edit_script_lines, insert_script_lines, or delete_script_lines for better precision and performance.

Parameters

instancePath
string
required
Roblox instance path to the script using dot notationExamples:
  • game.ServerScriptService.MainScript
  • game.StarterPlayer.StarterPlayerScripts.LocalScript
  • game.ReplicatedStorage.Modules.DataManager
source
string
required
Complete new source code for the script (must be valid Lua)Include all lines, indentation, and newlines exactly as they should appear

Response

{
  "success": true,
  "message": "Script source updated successfully",
  "lineCount": 156
}
success
boolean
Whether the operation completed successfully
message
string
Status message describing the result
lineCount
number
Number of lines in the new source code

Usage Examples

Replace Simple Script

{
  "instancePath": "game.ServerScriptService.WelcomeScript",
  "source": "-- Welcome script\nlocal Players = game:GetService('Players')\n\nPlayers.PlayerAdded:Connect(function(player)\n    print('Welcome', player.Name)\nend)"
}

Replace ModuleScript

{
  "instancePath": "game.ReplicatedStorage.Modules.DataManager",
  "source": "local DataManager = {}\n\nfunction DataManager.Save(player, data)\n    -- Save logic here\n    return true\nend\n\nfunction DataManager.Load(player)\n    -- Load logic here\n    return {}\nend\n\nreturn DataManager"
}

Replace LocalScript with Multiple Functions

{
  "instancePath": "game.StarterPlayer.StarterPlayerScripts.ClientHandler",
  "source": "-- Client handler\nlocal Players = game:GetService('Players')\nlocal player = Players.LocalPlayer\n\nlocal function setupUI()\n    -- UI setup code\n    print('UI setup complete')\nend\n\nlocal function connectEvents()\n    -- Event connections\n    print('Events connected')\nend\n\nsetupUI()\nconnectEvents()"
}

Best Practices

For scripts with many lines, use line-specific editing tools instead:
  • edit_script_lines - Replace specific lines
  • insert_script_lines - Add new lines
  • delete_script_lines - Remove lines
This is more efficient and reduces the risk of overwriting unintended changes.
Always ensure your source code is valid Lua before setting. Invalid syntax will cause runtime errors in Roblox.Common syntax issues:
  • Unclosed strings or tables
  • Missing end keywords
  • Invalid variable names
  • Wrong indentation levels
Maintain consistent indentation and formatting:
-- Good: Consistent indentation
function example()
    if condition then
        print('Hello')
    end
end

-- Bad: Inconsistent indentation
function example()
if condition then
print('Hello')
end
end
When passing source as JSON, escape special characters:
  • Newlines: \n
  • Tabs: \t
  • Quotes: \"
  • Backslashes: \\
Uses ScriptEditorService:UpdateSourceAsync() which:
  • Updates scripts even if they’re open in Studio
  • Preserves cursor position when possible
  • Triggers Studio’s change detection

When to Use vs Partial Editing

Use set_script_source when:

  • Creating a new script from scratch
  • Completely rewriting a small script (less than 50 lines)
  • Reformatting entire script structure
  • Replacing generated code

Use partial editing tools when:

  • Modifying specific functions or sections
  • Adding new functions to existing scripts
  • Fixing bugs in specific lines
  • Working with large scripts (>100 lines)
{
  "instancePath": "game.ServerScriptService.Small",
  "source": "-- Complete new script\nprint('Hello world')"
}

get_script_source

Read script source with line numbers

edit_script_lines

Replace specific line ranges

insert_script_lines

Insert new lines at position

delete_script_lines

Delete line ranges

Error Handling

{
  "error": "Instance not found or is not a script",
  "instancePath": "game.ServerScriptService.NonExistent"
}

Example: Rewrite Script

Example Script Before
-- Old version
print('Starting...')
wait(1)
print('Done')
MCP Request
{
  "instancePath": "game.ServerScriptService.TestScript",
  "source": "-- New version with improved logic\nlocal RunService = game:GetService('RunService')\n\nprint('Starting...')\ntask.wait(1)\nprint('Done')\nprint('Using new task library')"
}
Example Script After
-- New version with improved logic
local RunService = game:GetService('RunService')

print('Starting...')
task.wait(1)
print('Done')
print('Using new task library')

Build docs developers (and LLMs) love