How to add scripts to instant.new() parts?

good evening, how do i add scripts to instant.new() parts?

my current idea: maybe i could do

instance.new('part', workspace)
if game.workspace.name == new.part then (script)

but thats not working.
Additional Information:
game is about clicking generated parts for points against other players. you would be able to buy 1 time use items (such as swords to delay others)

these are my scripts:
press play button (only works if server is new)

local TextButton = script.Parent
local Player = game.Players.LocalPlayer

TextButton.MouseButton1Click:Connect(function()
	script.Parent.Visible = false
	Player.PlayerGui.ScreenGui.Frame.TextLabel.LocalScript.Disabled = false
	Player.PlayerGui.ScreenGui.Frame.TextLabel.Visible = true
	Player.PlayerGui.ScreenGui.Frame.new.Visible = false
end)

part spawners:

function brick(
)

wait(1)
script.Parent.Text = 'loading... 1 '
wait(1)
script.Parent.Text = 'loading... 2 '
wait(1)
script.Parent.Text = 'loading... 3 '
script.Parent.Text = 'error loading '
local Part = Instance.new("Part")
Part.Parent = game.Workspace
Part.Name = "click"
Part.BrickColor = BrickColor.Random()
Part.Material = "Metal"
local Poss = math.random(-500,500)
Part.Position =  Vector3.new(Poss)
local pos = Part.Position
script.Parent.Text = tostring(Part.Position)

end
brick()

note: all scripts are local; it would be ideal if a plus 1 point for every time you pressed instance.new() brick and leaderstat showing top player global and server script was included.

thank you

1 Like

To add a script to a instance.new(), I would recommend just coping a script already made and setting the parent to the Instance. I would also recommend keeping the script disabled until you move it to prevent errors.

3 Likes

i forgot how to make instance.new’s child a script. can you give an example

local ScriptToCopy = script.ScriptToCopy:Clone() -- The script you want to be in the Instance you're making
local Part = Instance.new("Part")

Part.Parent = workspace
ScriptToCopy.Parent = Part
ScriptToCopy.Disabled = false

Remember to keep it disabled until you move it to the new part or you will get an error.

2 Likes

can i not just have an existing one the brick uses? instead of cloning it

That’s the only way to add scripts to Instances through scripts.

and script to copy is the location of the script? i haven’t used clone yet sorrt

Yes. It’s the location of the script to get the script.

1 Like

testing it right now ok? i may ask more

thanks it works. have a nice day.

1 Like

Not necessarily but this is still good advice.