Making Plugins: The basics

Make a simple plugin.

by WWolfite

Author Avatar

Making Plugins: The basics


In this tutorial, I will show you how to make the most basic of plugins. A simple toolbar and a button that creates a script.

Steps


1. Foundation

Firstly, let's make a Folder in ServerScriptService. You can name the folder whatever you want. I typically name it the name of the plugin without spaces.

Now that you've made the folder, add a Script under it. For organizational purposes, you can name your script something like "Main." Your plugin should currently look something like this:

image|300x100 (Note: Icons may be different than yours.)


2. Scripting

Now you've set up everything necessary for now, let's get onto scripting. The first part of making a plugin is the toolbar. Before we can make any buttons, we need to add this. It should also be easily accessible in our script so we can add buttons to it later.

-- // Create the toolbar
local toolbar = plugin:CreateToolbar("Toolbar Name Here")

"local toolbar" declares the variable "toolbar." The equals symbol assigns a value to it. In this case it is out toolbar. By calling plugin:CreateToolbar(name:string) we are creating a toolbar (who could have guessed).

Next, we have to make our first button. What good is a toolbar without a button?

-- //  Create the first button
local button1 = toolbar:CreateButton("Button Name", "Button Description", "rbxassetid://11647603263")

Like before "local button1" declares the variable "button1," and we assign the button to that variable by calling toolbar:CreateButton(name:string, description:string, image:string). Did you notice we used our previous "toolbar" variable to create the button?

Now, we have to make the button do something! In this case, I'm going to make the button create a Folder. Not the most original or cool idea, but for now it'll have to do. Let's subscribe to the .Click event on button1 to begin.

button1.Click:Connect(function()
 
end)

Unlike UI Buttons and ClickDetectors, once a button is clicked it fires an event called .Click. Which in my opinion should be .Clicked because it makes more sense. Nonetheless, let's continue.

I could easily go on and make just a button that makes a Folder in the workspace, however, that's not very good at all. So, here are the main features:

Simple enough for now. Let's get to it.

First, let's call the services and assign them to varibles for easier reach.

local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Selection = game:GetService("Selection")

We need ChangeHistoryService to set waypoints for before and after we are done creating the folder. We also need Selection (a service too) to get what the user is currently selecting.

Back to our .Click event, let's set the first waypoint.

button1.Click:Connect(function()
	ChangeHistoryService:SetWaypoint("Creating new folder...")
end)

Good! Now let's declare a variable for our folder.

button1.Click:Connect(function()
 ChangeHistoryService:SetWaypoint("Creating new folder...")
 local folder = Instance.new("Folder")

Quite simple, you're getting the hang of this!

Next, we need to get what the user is currently selcting use Selection:Get() and set it the selected_objects variable. We also declare a variable called "parent" and set it's variable to the workspace. You can also change it to the default place you want the folder to be if the user is selecting nothing.

button1.Click:Connect(function()
	ChangeHistoryService:SetWaypoint("Creating new folder...")
	local folder = Instance.new("Folder")
	
	local selected_objects = Selection:Get()
	local parent = workspace
end)

Awesome! This next part may seem a little tricky, but you'll get it!

In the first line we cycle through if the select objects. We see what the user is selecting and how many. Then we find out if they are selecting more than 0 objects. In the next line we set the parent variable to the first object the user is selecting.

button1.Click:Connect(function()
	ChangeHistoryService:SetWaypoint("Creating new folder...")
	local folder = Instance.new("Folder")
	
	local selected_objects = Selection:Get()
	local parent = workspace
	
	if #selected_objects > 0 then
		parent = selected_objects[1]
	end
end)

Great! Not long to go from here.

Finally, we set the parent of the folder to the parent variable. We then add another waypoint telling studio that the action is complete and that we added the folder.

button1.Click:Connect(function()
	ChangeHistoryService:SetWaypoint("Creating new folder...")
	local folder = Instance.new("Folder")
	
	local selected_objects = Selection:Get()
	local parent = workspace
	
	if #selected_objects > 0 then
		parent = selected_objects[1]
	end
	
	folder.Parent = parent
	ChangeHistoryService:SetWaypoint("Added Folder.")
end)

Overall, your script should look something like this:

-- // Create the toolbar
local toolbar = plugin:CreateToolbar("Toolbar Name Here")

-- //  Create the first button
local button1 = toolbar:CreateButton("Button Name", "Button Description", "rbxassetid://1164760")

-- // Get the necessary services
local ChangeHistoryService = game:GetService("ChangeHistoryService")
local Selection = game:GetService("Selection")

--// Make the button work
button1.Click:Connect(function()
	ChangeHistoryService:SetWaypoint("Creating new folder...") -- Set the first waypoint
	local folder = Instance.new("Folder") -- Create the folder
	
	local sected_objects = Selection:Get() -- Get what the user is selecting
	local parent = workspace -- Make the parent variable (also what where the folder will be if the user is selecting nothing)
	
	if #selected_objects > 0 then -- Check if the user is selecting something
		parent = selected_objects[1] -- Set the parent varaible to the first thing they are selecting.
	end
	
	folder.Parent = parent -- Set the parent of the folder to the parent variable
	ChangeHistoryService:SetWaypoint("Added Folder.") -- Set the last waypoint, telling studio the action is completed.
end)

There we go! What a ride of scripting, hopefully you didn't run into too many issues.


3. Testing

Since you've worked so hard on this plugin, you probably want to try it out for yourself. That's easy! Right-Click your folder with your script in it (not the script itself). Now go down until you find "Save as local Plugin." Click that and save it. Now go over the the Plugins tab in studio. Find your toolbar and click the button.

image|100x300 Thanks for reading!

View in-game to comment, award, and more!