Roblox Code Automation with Lua Code Templates in Roblox Studio with AutoHotkey – Roblox Development #01

en-roblox001

Automation is key when developing software. That includes automating repetitive tasks like building pipelines but also coding and typing. One of the most productivity savers when coding are code templates and autocomplete.

Roblox Studio comes with autocomplete (which doesn’t work sometimes), but it doesn’t have code templates. But fear not, you can create your own code templates using text expanders.

AutoHotkey to the rescue

AutoHotkey is a very powerful free and open source Windows automation tool. It’s been around for quite some time and is used worldwide by all kinds of IT professionals (and non professionals as well). According to their website:

The ultimate automation scripting language for Windows. AutoHotkey is a free, open-source scripting language for Windows that allows users to easily create small to complex scripts for all kinds of tasks such as: form fillers, auto-clicking, macros, etc.

As a programmer myself, I’ve been using it practically since its launch in 2004 and I can say that I can’t live without it.

Go ahead, download and install it: https://autohotkey.com/download/. Official docs: https://autohotkey.com/docs/AutoHotkey.htm.

How to Create Expanding Words and Templates?

AutoHotkey has a feature called Hotstrings, which is what we are going to use. With Hotstrings, you define a text abbreviation which you want to be replaced with another word, phrase or even text with multiple lines. You can also simulate any keystroke (example: {enter}{left 2}, to place an ENTER and move the caret two times to the left).

Open Notepad or any other text editor and create a new file.

One of the most common lines in Roblox scripts is: local parent = script.Parentor normally declared as local sp= script.Parent. How to create a template for this line?

:*:xsp::local sp = script.Parent`n

Whoa! What is this nonsense? Hold on! It is easier than it looks:

:*: - this is telling AutoHotkey that this is a Hotstring
xsp - this is our abbreviation
::  - this is a separator between the abbreviation and the output
local sp = script.Parent`n - is what is going to be replaced when we type xsp. The backtick n (`n) means a new line.

Save the file and save it as roblox-templates.ahk. Double click to open it and execute AutoHotkey in the background. You can see the file running in the AutoHotkey icon in the system tray. You can pause and suspend the hotkeys anytime by right clicking the icon.

 

Also anytime you modify your .ahk file, you have to click “Reload This Script” over there.

How to Use the Expansions?

Simply open Roblox Studio and type any of your abbreviations. In our case, let’s test the one we just created. Type xsp in a Lua script. This is what happens:

Of course you can modify the abbreviation to anything that you want, such as /sp, localsp, scriptparent, etc.

Position the Caret Between Parenthesis

Say you have a template which places a method and then you want to type only the parameters after the expansion. This means moving the caret into the parenthesis. Simple: call {left [amount to move]} to simulate the LEFT key keypress.

:*:xwfc:::WaitForChild(""){left 2}

How to Replace an Abbreviation if you are Already Inside Another Word?

If you tried the above example exactly in the way that I did, it probably didn’t work for you, because spxwfc was typed and not xwfc. When you use :*, AutoHotkey expects the abbreviation by itself, standalone.

To make it work inside an existing word, like the above example, add a ? after :*. Example:

:*?:xwfc:::WaitForChild(""){left 2}

Now Anywhere I Type Something, AutoHotkey Replaces the Words!

With AutoHotkey and the command #IfWinActive, we can isolate abbreviations and templates to run only in certain programs! In your case, we can isolate our templates to Roblox Studio.

First, run AutoHotkey Window Spy to grab the application name: right click the system tray icon -> Window Spy -> make Roblox Studio the active program and then copy the line ahk_exe [Something], like I show here:

Now, in your .ahk script, place your templates inside a #IfWinActive return clause:

#IfWinActive, ahk_exe RobloxStudioBeta.exe
:*:xsp::local sp = script.Parent`n
:*?:xwfc:::WaitForChild(""){left 2}
return

Reload AutoHotkey (right click system tray -> reload this script) and you are ready to go!

Additional Tips

Raw Templates

If your template has characters that conflict with AutoHotkey’s commands and syntax, you can place them inside curly braces, specially colon (:). Example:

:*:xeq::tool.Equipped{:}connnect(){left}

Multi-Line Templates

You can use `n{Enter} or Continuation Sections.

Sample Templates

#IfWinActive, ahk_exe RobloxStudioBeta.exe
:*:localsp::local sp = script.Parent`n
:*:localtool::local tool = script.Parent`n
:*?:waitchild:::WaitForChild(""){left 2}
:*?:firstchild:::FindFirstChild(""){left 2}
:*:tooleq::tool.Equipped{:}connnect(){left}
:*:tooluneq::tool.Unequipped{:}connnect(){left}
:*:getservice::game:GetService(""){left 2}
:*?:econnect::{:}connect{(}{)}{left}
:*?:connectfn::{:}connect{(}function{(}{)}{Enter}{up}{End}{Left}
:*:mtuple::local tuple = {{}...{}}`n
:*:inew::Instance.new(""){left 2}
:*:v3new::Vector3.new(){left}
:*:cfnew::CFrame.new(){left}
:*:cfang::CFrame.Angles(){left}
:*:gwork::game.Workspace
:*?:brickcolor::.BrickColor = BrickColor.new(""){left}
:*:mrand::math.random()
:*:/delay::delay(, function{(}{)}{Enter}{up}^{left}^{right}^{right}{left 2}
:*?:/cf::CFrame.
:*?:/v3::Vector3.
:*?:/p::Parent
return

But is it Safe?

If you are worried about HotKey being a safe piece of software, check the following links. The community speaks for itself.

Its official forums have thousands of members and posts:

https://autohotkey.com/boards/

A Google search returns 3.8 million results, mostly tutorials, documentations and so on:

https://www.google.com.br/search?q=autohotkey&oq=autoh&ie=UTF-8

This article is also written in our Medium.

GOSTOU DO CONTEÚDO?
Faça parte da nossa newsletter e junte-se a mais de 3.000 pessoas que recebem nossos artigos, tutoriais e novidades por email e continue aprendendo a criar jogos e arte para jogos!
Odiamos SPAM! Seu email nunca será compartilhado.
FacebookTwitterGoogle+Compartilhar