Is there a way to make a script with a script?

So let’s say that you wanted to make a script from a script, how do you do that? Not like, making a script just by using Instance.new, but one that is made from another script that puts a script into the script if you know whatI’m talking about.

9 Likes

Your thread’s all over the place, so it’s somewhat hard to get information or understand your needs. Furthermore, why are you looking to “create a script with a script”? What’s your use case? I can’t think of any reason, other than for script builder games, where this is necessary.

Just look into threading via coroutines. Roblox has a Beginners Guide to Coroutines.

2 Likes

So, I’m not very good at scripting, and I would really want to enhance my scripting by making a game with only one script, and no cloning from premade scripts and whatnot, just a game using only one script. So, that’s why I need to know how to put lines inside of a script that’s made from an instance. And i don’t really get the coroutine thingy.

1 Like

What you’ve described is more commonly known as single-script architecture. This relies on a single script that’s considered the starting point for all code, which is known as a bootstrapper. All other code is hosted in ModuleScripts for which that main script requires in order to set up and run code.

If you’re new to coding, you probably want to focus on learning code first before getting into this architecture, as it may be tricky for newer developers. Luckily though, there are resources available that can help you get familiar with coding in the style. For example, @Crazyman32’s Aero Game Framework promotes this style. There are only two scripts (a server and a client one) that are responsible for setting up the framework and its utilities, then starting up other portions of code.

Since this type of question has been asked before, you should look into those threads and see if you’re able to gather any information or notes about coding in this style. I have answered this question in depth before - you can get some information there, provided you already know some terminology. Do use Google if you need to clarify some key terms.

Here are some other threads you may delve into:

9 Likes

You could put a modulescript as a model, and require() it, however I don’t really think this is a good way to learn how to code.

Other ways would be running code from http requests and a website(?)

1 Like

but, what I was asking was if i were to create a script from instance.new, and put code in the script

For Studio, yes you can create a script from Instance.new and write into it, but you can’t do that in a production game. I mean you could instance a script but it’d be blank and you’d be unable to append anything to it.

The main use case for being able to create and write into scripts in Studio via instancing is for plugins and quick script writing via the command bar. Scripts are represented as strings by their Source property, so modifying that would change what’s in the script. Again, only works in Studio.

5 Likes

You can’t edit the code of a script from another script, with the exception of the studio command line and plugins.

3 Likes

@colbert2677
@realhigbead
that’s a total bummer. Thanks for the help, anyway.

1 Like

Actually you can create a script using this, from a string:

All you need is a disabled script with a string value called “Script” in it. You set the value in another script and clone it.

-- ScriptBase
local Loadstring = require(...) -- replace with the path to the module
Loadstring(script.Script.Value)()
-- MainScript
local ScriptBase = script.ScriptBase
function loadString(string, parent)
local ScriptBase = ScriptBase:Clone()
ScriptBase.Script.Value = string
ScriptBase.Disabled = false
ScriptBase.Parent = parent -- whatever parent
end

loadString("print('Hello World!')", workspace)
14 Likes