Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 2 years 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.

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. greatneil80 2647 — 2y
0
oh not to mention you can use loadstring but that will allow every exploiter in existence to ruin your game greatneil80 2647 — 2y
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. Stiles8353 35 — 2y
0
i gave you every possible answer to this question greatneil80 2647 — 2y

2 answers

Log in to vote
0
Answered by
manith513 121
2 years ago
Edited 2 years 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)
    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.

0
If you want an example, let me know. manith513 121 — 2y
0
Yes I would like an example please. Stiles8353 35 — 2y
0
Your example but what variables are you trying to use in one script to another? Modules can also call variables like you want. manith513 121 — 2y
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. manith513 121 — 2y
Ad
Log in to vote
0
Answered by
NykoVania 231 Moderation Voter
2 years 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)

modulething.Swag("hello")

Module Script:

local module = {}

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


return module
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. Stiles8353 35 — 2y
0
You could try a global variable. NykoVania 231 — 2y
0
_G shouldn't be used anymore. Anything _G can accomplish, modulescripts can do as well but better. manith513 121 — 2y

Answer this question