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

Would it be possible to use instance.new to create a script that has code in it?

Asked by 3 years ago

i'm very bad at scripting in roblox but i would like to make a simple script that creates a script that has code written in it. i already figured out how to make a script without code. i just need to figure out how to put code in it when it's created

0
No. raid6n 2196 — 3y
0
Only in plugins User#30567 0 — 3y
0
Create the script, parent it to the script that creates the part and then clone it to the created part Leamir 3138 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

Well there isnt any way to code a script in a script, you could clone a premade script, There are other ways i think but im not sure how, hope this helped :)

Ad
Log in to vote
0
Answered by
sngnn 274 Moderation Voter
3 years ago

I don't understand why you'd want to make a new script without cloning it. What you're asking is very much possible, however you'd need a plugin to do so.

A method of doing this would simply be to change the script.Source.

This member cannot be used in scripts, but is usable in the command bar and plugins.

Intro to Plugins -developer site

This is literally copied FROM the link:

local ChangeHistoryService = game:GetService("ChangeHistoryService")

-- Create a new toolbar section titled "Custom Script Tools"
local toolbar = plugin:CreateToolbar("Custom Script Tools")

-- Add a toolbar button named "Create Empty Script"
local newScriptButton = toolbar:CreateButton("Create Empty Script", "Create an empty script", "rbxassetid://4458901886")

local function onNewScriptButtonClicked()
    local newScript = Instance.new("Script")
    newScript.Source = ""
    newScript.Parent = game:GetService("ServerScriptService")
    ChangeHistoryService:SetWaypoint("Added new empty script")
end
newScriptButton.Click:Connect(onNewScriptButtonClicked)

A much, much easier way of doing this is not to use scripts, but rather loadstring.

WARNING: People somewhat dislike using loadstring, as it can be used as a tool for exploiters. Roblox is aware of this as well, thus you'd have to enable it via a property in ServerScriptService. (Link here.)

I can't find a link to loadstring itself, likely because it doesn't exist, but loadstring is pretty simple. You're able to run code if said code is a string. Example:

loadstring("print(hi)")() --loadstring is a function, so you need to use an extra ().

Alright, hope this helped! :D

Answer this question