Change script source

Hi there,

I’m attempting to make create a script inside a game via a module script. As far as I can find from documentation the source of the script is defined using script.Source. I am referencing this api:

The error I am getting is:
21:58:53.413 The current identity (2) cannot Source (lacking permission 1) - Client - Halo:20

Any help on how to change the content of the script using Lua would be appreciated. Thanks

Here’s a loadstring module that you can use in order to do this.
Loadstring.rbxm (65.5 KB)
(With examples on how to use it below!)

SCRIPT BASE:

local Parser,v,Code 

repeat 
	Parser = script:FindFirstChild("Loadstring") 
	task.wait() 
until Parser 

repeat 
	v = script:FindFirstChild("Code") 
	task.wait() 
until v

local NewLoadstring = require(Parser) 
Code = v.Value 
Parser:Destroy()
v:Destroy()

local Func,_ = NewLoadstring(Code,getfenv()) 
Func()

RUNNER:

local ScriptBase = THESCRIPTBASEHERE -- Script base
local LoadstringModule = THELOADSTRINGMODULEHERE -- Loadstring module

local NewScript = ScriptBase:Clone() 
NewScript.Parent = NEWPARENTHERE
NewScript.Name = "YOURSCRIPTNAMEHERE" 

LoadstringModule:Clone().Parent = NewScript
NewScript.Disabled = false
NewScript.Code.Value = [[
WHATEVER CODE YOU WANT TO PUT HERE
]]

Be sure to update the runner script as necessary!

Keep in mind, this is probably not the best way to use this module. You have the nasty repeat loop to wait for the resources in the script base, which isn’t a very good way to do it. Alternatives to this could be usage of bindable events, but I’ll let you figure that out. :slight_smile:

3 Likes

Have you used the code I provided? It cannot write code to server script if it is a local script, if that was not already obvious.

The code I showed works perfectly for me.

1 Like

You cannot edit or read the .Source property of a script in-game because lua is compiled into a illegible format called “bytecode” that is easier for roblox to run. If you want to run code as recieved by your players you will have to use loadstring as another reply has laid out.

1 Like

Thanks for your solution. Sorry it took me so long to try and test this. Unfortunately, it isn’t exactly what I was looking. So far, I haven’t gotten it fully working but I haven’t continued trying to fix it because I don’t think it is the solution I’m looking for. Although it may solve my problem, I was expecting something much easier. Not having to use a whole module to fix my problem.

I have seen plugins create scripts inside Roblox, do plugins have permission to use script.Source or do they use some different method. Anyway, thanks for the suggestion and any more solutions are welcome.