Overview
Replace the entire source code of a Roblox script. UsesScriptEditorService:UpdateSourceAsync() to work seamlessly with open script editors in Roblox Studio.
Parameters
Roblox instance path to the script using dot notationExamples:
game.ServerScriptService.MainScriptgame.StarterPlayer.StarterPlayerScripts.LocalScriptgame.ReplicatedStorage.Modules.DataManager
Complete new source code for the script (must be valid Lua)Include all lines, indentation, and newlines exactly as they should appear
Response
Whether the operation completed successfully
Status message describing the result
Number of lines in the new source code
Usage Examples
Replace Simple Script
Replace ModuleScript
Replace LocalScript with Multiple Functions
Best Practices
Prefer Partial Editing for Large Scripts
Prefer Partial Editing for Large Scripts
For scripts with many lines, use line-specific editing tools instead:
edit_script_lines- Replace specific linesinsert_script_lines- Add new linesdelete_script_lines- Remove lines
Validate Lua Syntax
Validate Lua Syntax
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
endkeywords - Invalid variable names
- Wrong indentation levels
Preserve Formatting
Preserve Formatting
Maintain consistent indentation and formatting:
Handle Special Characters
Handle Special Characters
When passing source as JSON, escape special characters:
- Newlines:
\n - Tabs:
\t - Quotes:
\" - Backslashes:
\\
Works with Open Editors
Works with Open Editors
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)
Related Tools
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
Example: Rewrite Script
Example Script Before
MCP Request
Example Script After