How to insert an object into the selected part with a plugin?

I made a plugin in which I can insert objects that I use often (such as scripts and GUIs) which are already preset, so that I won’t have to do it every time.

I’m wondering if there’s a way to insert the object into the part that the player has currently selected in the workspace, instead of inserting it in the workspace itself.

This is how I currently create the items:

item1Insert.MouseButton1Click:connect(function(Item1)
	local gui = Instance.new("SurfaceGui", workspace)
	local label = Instance.new("TextLabel", gui)
		
	gui.SizingMode = "PixelsPerStud"
	gui.PixelsPerStud = 150
	gui.Face = "Front"
	label.Size = UDim2.new(1,0,1,0)
	label.Position = UDim2.new(.5,0,.5,0)
	label.AnchorPoint = Vector2.new(.5,.5)
	label.Text = "Text"
	label.TextScaled = true
	label.BackgroundTransparency = 1
end)

Is there a way to set the parent of, in this case, the GUI to the selected part, instead of the workspace?

1 Like

This will get the Instance you have selected in explorer.
local Selected = game:GetService("Selection"):Get()[1]

1 Like

Keep in mind that it’s unwise to assume that you have something selected, it’s unwise to assume that you only have one thing selected, and it’s unwise to assume that whatever is selected is something that you should be messing with.

In other words: Code your plugins responsibly.

1 Like

Thank you!

Do you happen to know a way to select the GUI after it is inserted?

Use [Array<Instance>] Selection:Get() and Selection:Set(Array<Instance>)

1 Like

selection:Set rather than Get.

Set accepts a table with numeric indexes, also known as an array, just as Get returns one.

I was planning on using this plugin myself only, so then I can keep in mind that I can only select one thing

But wouldn’t you be so much more proud of yourself if you actually made a good plugin instead of a hack job? :D

Just a thought. Not saying your plugin is a hack job, just saying that if it’s not, you’d be happier than you would if you had a hack job :stuck_out_tongue:

Well I suppose everyone has to start somewhere

1 Like

I see sorta what he means i guess. I had a private plugin to hackily select objects, but then i updated it to do things like select all instances of the same color or material etc within a selection and made it public