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)
39 Likes

Good breakdown. Using camelCase for variables/functions, PascalCase for modules/classes, and LOUD_SNAKE_CASE for constants makes code easier to read and maintain. Consistency matters more than the style itself, and the note on acronyms is useful. Solid guide for Roblox projects.

1 Like

That bias.
It doesn’t metter how you name anythings it will eventually turn into upvalues/registers in bytecode.
Also stop misusing word “professional” to be corporate.
Professional means originally competence and thing you are saying are your personal opinion.

1 Like

This isn’t bias nor is it my personal opinion, if you take time to actually read this topic you would see that I linked the roblox style guide and all pieces of information on this topic strictly follow it aswell.

As I also mentioned in my post:

The point of sticking to the style guide is merely for readability and nothing else, you pointing out that it will eventually “turn into upvalues/registers in bytecode” is nonsensical as nobody is going to be reading the raw bytecode.

2 Likes

Still doesn’t matter.
Just don’t name values something like :HeLlO, and it’s already pretty readable.
If something doesn’t influence produced bytecode or optimization its completelly ilerevant
I legit see people criticize someone’s code for using Snake case instead of Pascal case.
Im not really a fan of such “style” cults.

1 Like

I would aruge that its subjective. But in a professional enviorment it’ good to have consistency. And using different casings to signify what type of data they are used as. Can really help development on the game / module. If the code your developing is only going to be used by you then this isn’t needed.

That doesn’t mean OP shouldn’t make a topic covering when to use the different casings. Id argue its really helpful for people getting into more professional enviorments!

3 Likes

What do you mean by “professional environment”? Please define it.

Speed and performance > consistency and other corporate ahh stuff.

It’s just empty promises
Real devs don’t care about any of that; they are a highly trained professionals.
I just don’t like empty corporate promises that mean nothing from the beginning to the end.

this has nothing to do with bytecode why even bring it up?

you don’t have to use the same cases that were said in this topic (highly recommended to use them and Roblox also suggests these cases in the best practices doc)

but you have to be consistent it isn’t any good to name a local variable in full lowercase and then another one in full uppercase

that helps you easily differentiate and know what your variables are supposed to represent without digging through your code wasting your time

for instance, a variable named MAX_HEALTH easily tells that it is a constant, while a variable named maxHealth tells that it could be changed

for instance look at code like this

local MAX_HEALTH = 100

local health = 10
local healthRegen = 3

function takeDamage(damage)
    health -= damage
end
function heal()
    health += healthRegen
end

you can clearly see that the code is clean and that it explains itself, you know what the purpose of each variable is without needing to dig through the code
vs smt like this

local maxhealth = 100

local HEALTH = 10
local health_Regen = 3

function DAMAGE(Damage)
    HEALTH -= Damage
end

function heal()
    HEALTH += health_Regen
end

which is clearly messy, and fails to explain itself let alone trying to guess what case that variable was written in

if you are going to name local variables in snake case then stick with it, don’t name them once with snake case and another time with camelCase unless code readability isn’t a priority for you

and if you are going to release a library for other people to use it is recommended to write with the convention cases that Roblox and most people recommend and are already comfortable with

3 Likes

May you please give me one instance where using cases affects the performance and or speed of your code?

and what do you mean by “corporate stuff”?

what do cases have to do with corporations?

1 Like

By that i meant empty promises and loud titles.
You should’ve already knew about this term if you don’t want to be tricked into being victim of “ultra optimized framework” or fully contradicting itself titles.

Professional enviorment = An enviorment where your code is going to be used, edited and looked at. And not just at the time thr code is worked on but maybe in the future.

In thosse situations it’s good to keep code consistent so other developers that edit and use your code know what to expect. Or when multiple devs work on smthing they both agree on how the structure of the code will look like.

1 Like

i think i get it, it’s to identify something quickly. Example: local PathfindingService = game:GetService("PathfindingService") etc. When you read the name, you’ll already know it’s something related to classes or modules, so you don’t have to waste time reading the whole thing to try to know what it means and how to apply.

1 Like