Roblox LSP
Roblox LSP is Lua Language Server for Roblox forked from sumneko’s Lua language server.
Everything you will see in this topic and more was made by me, but this would not be possible without the language server created by sumneko.
Introduction
This project was made with Roblox developers in mind.
Roblox LSP provides a lot of features, like advanced IntelliSense, Signature Help, Goto Definition, Symbols, Diagnostics, etc, and many focused on programming in Roblox and Luau.
When you are a Roblox programmer, you may not be satisfied with Roblox Studio and the Script Editor it has for various reasons, so you decide to use an external editor (like Visual Studio Code) to improve your workflow.
This comes with many advantages like:
- Have your project in your filesystem.
- Use Git for version control (Especially useful when working with teams)
- Use your favorite code editor, and this comes with more advantages like Snippets, Multiple Editors, Multiple Cursors, Code Preview, and a huge amount of Plugins that give you even more!
Now you can add to that list: A Better IntelliSense!
I am going to show you its features and explain you how to use it, so this topic can become a little long.
sumneko’s Lua Features
- Inlay Hints
- Goto Definition
- Find All References
- Hover
- Diagnostics
- Rename
- Auto Completion
- IntelliSense
- Signature Help
- Document Symbols
- Workspace Symbols
- Syntax Check
- Highlight
- Code Action
- EmmyLua Annotation
- Multi Workspace
- Semantic Tokens
Features
Roblox Environment
You will feel that you are programming in Roblox Studio (but better), it has an environment exactly same as Roblox, you have the same globals, same functions, same everything!
And you can also expect certain behaviors, such as that global variables can only be used in the script in which you define them, and that _G
and shared
are shared across all scripts.
Classes and Datatypes
As you can expect, you will have full intellisense for each class and datatype, but not only that.
You will get documentation and details of the API of almost every member of every class.
And also className autocompletion for functions like GetService
, IsA
, etc, and Enums autocompletion!
Functions like GetService
, Instance.new
, FindFirstChildOfClass
, etc, returns the type of the arguments you give them, for example:
local basePart = game.Workspace:FindFirstChildWhichIsA("BasePart")
returns BasePart
.
When giving a function as an argument to the method Connect
of a RBXScriptSignal
, the parameters of the callback will have their corresponding types!
And you also have a snippet that autocompletes the function and the parameters.
When doing math operations with certain types, you will get the expected type!
You also have some table types, like Dictionary, Array, Map, and Objects, which will give types to the parameters of a for loop.
Dictionary gives you string
as key and any
as value.
Array gives you integer
as key and any
as value.
Objects gives you integer
as key and Instance
as value.
But that’s not all, because some functions gives you more specific types, for example, Players.GetPlayers
will give you Objects<Player>
Full Luau Syntax
It supports full Luau Syntax! and has basic IntelliSense for Compound Assignments, Continue Statement and types for parameters, variables and returns.
However, there is not type checking, but i’m planning to implement it in a future update.
Using Selene (Optional)
Selene is a linter created by @Kampfkarren. In order to use it, disable Roblox LSP diagnostics.
Rojo Support
This is extremely important and useful, with support for Rojo you can do a lot of things!
If you have a project file in your workspace, Roblox LSP will read it and you will get IntelliSense for modules, it will know the location of the scripts, and you will get Autocompletion for children in DataModel that you created in your filesystem.
It has support for .txt, .lua and .model.json files, and recognizes init files too.
If you have multiple projects in your workspace, you can set Roblox LSP > Workspace: Load Mode
to Rojo Project
, and Roblox LSP will only load the scripts that can be found in your Rojo Project, unless you open them manually.
You also have to specify the name of the .project.json file you are working with in Workspace: Rojo Project File
, by default it is “default”.
But if you want to work on several projects at the same time that are in the same workspace, and you have several .project.json files, then you can set Roblox LSP > Workspace: Rojo Project File
to blank, this will make Roblox LSP read all the .project.json files it finds, convert them into Instance Trees, and then combine the Instance Trees into one.
You can also add an special annotation on top of variables to get intellisense of a module that Roblox LSP didn’t found.
---@module path.to.test
local test = require(test)
Another thing to notice is that the functions FindFirstChild
and WaitForChild
, will return you the child of the Instance if it finds it, otherwise, they will return you a value of type Instance
.
Autocompletion for datamodel descendants
Roblox LSP can receive information from Roblox Studio using a plugin, if you install it, a list of every descendant in your game will be send to Roblox LSP every time you create, remove, move or rename an Instance.
To use this feature, install the plugin here, and two buttons will appear in the Toolbar called Connect to start sending data to this extension, and Settings to configure the plugin.
Roblox LSP will be ready to receive data when you initialize the extension (when you open a lua file), it creates a port in localhost (your machine), you can configure the port changing the setting robloxLsp.misc.serverPort
. You can also configure the plugin to make it start automatically when you open the place, exclude instances and their descendants, or change the port, both ports must match. It is recommended to use a different port for every project.
You can check if the data is being sent to localhost in http://127.0.0.1:PORT/last.
Children of StarterPack, StarterGui, StarterPlayerScripts and StarterCharacter will also be children of Backpack, PlayerGui, PlayerScripts and Character respectively.
Color Picker and Preview
When you call the constructors of Color3, a box will appear that you can use to preview the color and by clicking on it, change it easily.
(The gif recorder does not capture all colors of the gradient).
Using it with other Code Editors
If you prefer Sublime Text, Atom, Vim, etc, luckily, as Roblox LSP is a Language Server, it can be used in almost any code editor.
To do it, all you need is the server that you can found in the GitHub repository, and a Language Client for your Code Editor, the process is completely different for each code editor, so I can’t explain it here.
Keep in mind that depending on the code editor and the language client you use, some features of Roblox LSP will not work correctly or will not be available, the most recommended is to use Visual Studio Code, which already comes with an extension to install it easily.
Get Help
You can join the Roblox OSS Discord Server to get help if you have a problem.
Notes
-
Make sure you don’t have both Lua by sumneko and Roblox LSP installed.
-
Roblox LSP consumes a lot of RAM, but nothing worrisome (unless you have like 2 GB of ram).
-
The Language Server can only run on 64-bits computers.
-
If you have a Low-End computer, you will start to notice it slow from 700 lines of code, the performance depends only on the file you are editing, and there are other factors such as the complexity of the code.
-
When the Language Server starts, it will load all the scripts in your workspace one by one (depending on how
Workspace: Load Mode
is configured), this process may be slow, but the first script that will load will be the one you are editing and from there you can start coding. -
The Roblox API updates automatically.
-
This topic may have many grammatical errors.