Hello everyone. So, I’m trying to make an automatic script repair for my game. Basically it will fix old scripts by rewriting instance functions with wrappers. The problem I’m having is that I can’t figure out how to read/write the source of a script. I would use loadstring but apparently it makes your game more vulnerable to exploiters, and on top of that I don’t even know if loadstring would work anyway.
Loadstring doesn’t inherently make your game vulnerable to exploiters: the issue is that if backdoor code relies on loadstring and you have it enabled, then that becomes a problem because that opens the possibility of arbitrary code being ran on the server.
If your intention is to change a script’s source from in-game, you won’t be able to accomplish this - you will require a Lua-in-Lua VM solution or, as earlier mentioned, loadstring with a source to load from. You’re in luck if it’s just from Studio though, since Source can be used by plugins and the command bar.
The Source property of LuaSourceContainers (Script, LocalScript, ModuleScript and CoreScript) is a string representation of the script’s contents. With this, you can apply various string patterns to the returned source or write to it as you wish. With this in mind, be careful and mindful when you’re modifying script sources, as you can get easily lost or run into unintended changes while modifying a giant multi-line string.
LuaSourceContainer is the superclass of the objects in the brackets. Just focus on the actual concepts that I’ve put forth as they relate to modifying a script’s Source property.
Source is a representation of the entire script’s contents in a string
You can modify this string and apply it to Script.Source to change what’s in the script
With the above in mind, you can use string patterns to replace various text
Be mindful of working with a script Source as it is a long multi-line string and can trip you up
The other comments I made are in reference to running arbitrary code in-game (meaning scripts that are “created” and ran at game time rather than existing when you’re in Studio developing) but that’s probably not what you’re looking for.
Only plugins and (I think) the command bar have the required level to write to a script’s source. It is impossible to do that in-game for obvious reasons.
I’m sure there’s a way to write somehow because I’ve seen kohl’s admin create local scripts from scratch. Are new scripts are able to be source edited?
Basically the idea is to make a script that fixes broken scripts that broke over time to help people who aren’t as experienced in scripting. I decided to start when I saw that loadlibrary was going to be removed so I could help out my friends and any others who might need help. It will not only fix scripts that break due to removal of loadlibrary, but it will also fix scripts that broke because of fe being forced. For example: if a server script attempts to get the player’s mouse, my script repair will create a fake mouse instance that actively copies or mimics the player’s mouse instead of just returning nil or Vector.new(0, 0, 0) if the position was called. It will be a simple way to fix broken scripts for new scripters who don’t know how to fix it them selves, and an easy shortcut for experienced ones who don’t want to spend so much time fixing all the scripts manually.
It’s a cool idea in theory and I wish you luck with it, but be warned that its going to be a lot harder to do this than you first think. For example, how will you determine if they are using one of these methods? You could parse the script with AST or something, but it will still create even more compatibility issues than the ones it fixes (I think). How will you actually put the fixes into the script?
It’s a really cool idea and I can’t wait to see where it goes, but be warned that doing this sort of thing with a programming language properly is very very difficult.
You are able to do this but only in studio. Also FYI, the loadstring warning is mostly there to ward off inexperienced scripters since it’s very dangerous if you don’t use it right. It’s easy to introduce vulnerabilities with loadstring hence the warning.
In the command bar and plugins a script’s Source property can be modified. Due to security issues you aren’t able to read the Source property in a live game. I think the property stores an empty string in that case.
LuaSourceContainers are essentially the BasePart of scripts. All scripts extend from this class meaning script:IsA("LuaSourceContainer") should always be true.
You may want to look at a lua parser like the one used here. Note: A parser does not execute lua code, but rather breaks it down into a structure that can be understood much better. This one in particular is well written and offers example code modification (it minified lua code aka replaces all variable names with useless ones and reduces whitespace). You’ll need to make a few modifications to have it work in Roblox though. Requires are a good example and thankfully it doesn’t depend on any extra libraries.