How can you find a string from a script then replace that string from a script?

How can you find a string from a script then replace that string from a script?
This is for a plugin.

You’d have to use the Source property of a script and then use string.gsub

Example, say you have a script with this

print("Hello world")

And you want to turn world into earth, you can use code similar to this in the command bar or from a plugin if you want to do it via code

local newSource = string.gsub(workspace.Script.Source, "world", "earth")
workspace.Script.Source = newSource

Or in 1 line if needed

workspace.Script.Source = string.gsub(workspace.Script.Source, "world", "earth")

And it should work hopefully, assuming that the script is in workspace

Edit: Okay I didn’t read the title properly since you want to do it from a script, there’s no real way to do so as the Source property is only accessible from Command bar or plugins, because it is a ProtectedString, likely due to security reasons

If you want to find a string from a script and replace it, you have to use the Command bar, make a plugin, or find and replace

1 Like