Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

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

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.

4 comments

0

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.

0

oh not to mention you can use loadstring but that will allow every exploiter in existence to ruin your game

0

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.

0

i gave you every possible answer to this question

2 answers

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)
	print(textyouwanttoprint)
end,
}

In the normal script

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

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.

4 comments

0

If you want an example, let me know.

0

Yes I would like an example please.

0

Your example but what variables are you trying to use in one script to another? Modules can also call variables like you want.

0

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.

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)

modulething.Swag("hello")

Module Script:

local module = {}

module.Swag = function(t)
print(t)
end


return module

3 comments

0

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.

0

You could try a global variable.

0

_G shouldn't be used anymore. Anything _G can accomplish, modulescripts can do as well but better.

This discussion is preserved as it was. New answers, comments, and votes are closed.