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

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

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

3 comments

0

No.

0

Only in plugins

0

Create the script, parent it to the script that creates the part and then clone it to the created part

2 answers

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 :)

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

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