Understanding When to Use Different Cases in Luau Scripting

Hey fellow programmers!

I wanted to talk about the ways we name things. How we write them in Luau scripts. Naming conventions play a role, in making our code easy to understand, update and look professional. Lets take a look at the styles of naming and when its best to use them in Luau scripting, for Roblox projects.

1. camelCase

CamelCase is a popular naming convention where the first word is in lowercase, and each word after starts with an uppercase letter. It looks like this: myVariableName.

When to use camelCase:

  • ->Local<- Variables: Most commonly used for local variables and non-constant global variables.
    • Example: playerHealth, enemyCount.
  • Function Names: Frequently used for function names.
    • Example: getPlayerScore(), updateEnemyPosition().
  • Member Values: i.e. numberValue, BooleanValue (instances)
    • Example: playerKills, isFlying.
  • File Names: Your file name should be formatted in camelCase
    • Example: antiCheat, playerSprint.

Example:

local playerHealth = 100
local function updatePlayerHealth(amount)
    playerHealth += amount
end

2. PascalCase

PascalCase is similar to CamelCase, but the first letter of each word, including the first word, is capitalized. It looks like this: MyClassName.

When to use PascalCase:

  • Class Names: Ideal for naming classes, modules, or constructors.
    • Example: PlayerController, GameService.
  • Module Scripts: Commonly used for naming module scripts to differentiate them from other objects.
  • Exporting Types:
    • Example: Actions, States.
  • Services: Used for naming services, especially when referencing Roblox services.
    • Example: DataStoreService, Players, ReplicatedStorage.

Example:

local DataStoreService = game:GetService("DataStoreService")

local DataManager = {}
function DataManager:SaveData(player)
    -- logic for save data
end

return DataManager

3. LOUD_SNAKE_CASE

This is a variant of snake_case where all letters are uppercase. It looks like this: MAX_PLAYER_COUNT.

When to use LOUD_SNAKE_CASE:

  • Local ← Constants: Perfect for defining constants in modules that are not meant to change.
    • Example: MAX_HEALTH, DEFAULT_GRAVITY.

Example:

local MAX_HEALTH = 100
local DEFAULT_GRAVITY = Vector3.new(0, -196.2, 0)

4. Acronyms

For acronyms within names you should not capitalize the whole thing, i.e. makeHttpRequest(), JsonTable

The exception to this is when you are abbreviating a set. As an example:
aRGBValue, XYZCoordinate - These are fully capitalized as they should be treated as an abbreviation and not an acronym.

5. File Names

The name of your file

(your script)

should be represented (named) by what it does, for example:

A script that exports a single function named findPlayerLocation should be named findPlayerLocation.lua (without the .lua extension at the end)

Best Practices

  1. Consistency: Regardless of the case you choose, ensure consistency throughout your project. If your team decides on CamelCase for variables, stick with it.
  2. Code Standards: Not all people will use these cases properly, if you are coding a script after it has already been made and the cases used are inproper / incorrect, make sure to go with the flow and try not to change too much.
  3. Readability: Prioritize readability. Naming stuff properly should help clarify what a variable or function does, not obscure it.

By understanding and applying these different case styles appropriately, it will help make your code more readable and understandable by other programmers (or even yourself in the future).

Source: Roblox Lua Style guide ← (for more advanced folks)
21 Likes