I would like to change the content of a LocalScript from another LocalScript.
What is the issue?
You can’t change the source property of a LocalScript from another LocalScript.
What solutions have you tried so far?
I’ve looked on the DevForum however couldn’t find any similar issues. I’ve also looked at the documentation.
If it’s not possible, what other method would work for your experience?
If you could somehow execute the contents of a StrValue from a LocalScript, then that would also work. I’m basically just trying to make a TextBox that, whenever changed, changes the contents of the LocalScript, so whatever would work for that would be good.
Make multiple scripts, parent them under you original local script. Once changed text property, delete the current local script that’s binded, and clone a new one to bind to the label.
That sounds great, but how do you create a new script with custom content? I mean like making a script that doesn’t start with print(“Hello world!”) but instead starts with like print(“hi”) or something?
You can run code from a string with the loadstring function, however the function can only be used on the server, and you want to do this on the client. Instead, you can use the vLua5.1, which is a virtual machine for compiling and executing Lua code regardless of whether or not loadstring is enabled in your game or if it’s being executed on the client or server. It should be noted that it’s not very good handling longer code. You can get the module here: https://create.roblox.com/marketplace/asset/4689019964/vLua-51-improved-VM
Once you have it in your game you should try executing some basic code with it:
local loadstring = require(path.to.module) --wherever you put the module in your explorer
local code = "print('Hello, world!')"
local executable, err = loadstring(code)
if err ~= nil then warn(err) end
executable()
This script should print: Hello, world!
Hopefully this helps!
There’s a few things linked in the main script, probably don’t click those, this is just from module I found