Log in to vote

Is it possible to copy code from a script and paste that code into another script?

Asked by 22 days ago

The title may be a bit confusing so here's a better breakdown of what I am asking.

Is it possible to, for example take script 2's code

-- hi this is script 2

and copy and paste it using script 1

-- hi this is script 1

   local script2code = game.ReplicatedStorage.Script2:CloneCode -- just an example 

script2code:PasteCode -- also just an example

I've looked around in module scripts a little bit and I'm pretty sure they're not what I'm after.

Any advice, and potential answers are appreciated.

you can clone a script, you can use remote events to do things from the server to client, you can ctrl c v, you can use "require()" for module scripts then run functions in other scripts, you can use .Source = "code" assuming ur working in the command bar and plugins. greatneil80 2641 — 22d
oh not to mention you can use loadstring but that will allow every exploiter in existence to ruin your game greatneil80 2641 — 22d
No I don't mean like cloning the script normally, I mean taking code from a script, and importing it into the script that went to copy it. Stiles8353 10 — 22d
i gave you every possible answer to this question greatneil80 2641 — 22d

2 answers

Log in to vote
Answered by 22 days ago
Edited 20 days ago

Copy and pasting a function from one script to another, yields the same result as calling that function in another script using a modulescript. I don't think copying the text is possible unless you make a complex function that takes parameters to do so. Your best option is to just use a module script to use the code in that module script in other scripts (which is the same result as what you are trying to do.)

the code below is in a module script. Pretend the name of this script is "PrintModule" and its in the workspace.

local module = {

yourfunction = function(textyouwanttoprint)

In the normal script

local module2 = require(game.Workspace.PrintModule)
local texttoprint = "ModuleCalled"

The output will have printed "ModuleCalled"

The function in the module can be replaced with pretty much anything, if you are looking to call that function in other scripts. You want to copy code from one script to another, instead of that this puts code in a module script which can be called from another script. Which runs that code in the script you called it from.

If you want an example, let me know. manith513 61 — 22d
Yes I would like an example please. Stiles8353 10 — 22d
Your example but what variables are you trying to use in one script to another? Modules can also call variables like you want. manith513 61 — 20d
In the module if you do. local object = script.parent.thing2 then in other scripts you can just do module.object and it will return the same thing. manith513 61 — 20d
Log in to vote
Answered by
NykoVania 198 Moderation Voter
21 days ago

Use a ModuleScript, they are essentially just extensions of the script that runs them. Here is an example:

Normal Script:

local modulething = require(module location or module id)


Module Script:

local module = {}

module.Swag = function(t)

return module
Yeah I've tried to do that but the issue is that I need my variables, for example, local thing = script.parent.thing2, which would only work if the code was directly input into the script before, otherwise it throws an error because it is reading script.parent as modulescript.parent. Stiles8353 10 — 21d
You could try a global variable. NykoVania 198 — 21d
_G shouldn't be used anymore. Anything _G can accomplish, modulescripts can do as well but better. manith513 61 — 20d

Answer this question