Wut a constants?

whats a constant? there usally all in Upper case and have underscores when there are multipul words
here is an example:

local AMONG_US = 'AMOGN US'-- amung us beat boxing in the background

what are they for?

Constants are basically just read-only variables. Lua doesn’t have any though so this is more just an indication not to set this variable

4 Likes

These are called variables. They are things that can be used in code for easy access to specific text/code/anything else. They are essentially shortcuts.

Here’s an example:

local map = game.Workspace.Map

print(map.Name)
1 Like

you mean not to change sorry i have -1 brain cell

i know what a variable is lol i am no noob

dude then why did you ask

did you srsly ask for no reason

If you’re talking about the underline: I suppose it’s just personal preference

Usually some people would define their variables like these:

local RUN_SERVICE = game:GetService("RunService")
local MARKETPLACE_SERVICE = game:GetService("MarketplaceService")

I think it’s because they’re used to using a whole different programming language, such as C+ & Python but I could entirely be wrong

Although this is valid, you still should be careful when using underlines such as _VERSION and etc since those are reserved

i am asking what a constant is i know its a variable i am asking why is it called a constant maybe because its a specials variable idk so that why i ask

A constant is literally the exact same thing as a variable; it just has different wording

1 Like

Yeah, the underline is more personal preference. Also, I think some people prefer to use variable names in all caps for settings or something similar.

why it called a constant then?

Actually a quick search shows this about a constant:

“A fixed value. In Algebra, a constant is a number on its own, or sometimes a letter such as a, b or c to stand for a fixed number. Example : in “x + 5 = 9”, 5 and 9 are constants”

So I suppose they’re just basically values that can’t be changed

4 Likes

so constants are just an indication this variable is not to change

1 Like

Lua doesn’t have any constants, that was what I was saying

I suppose so, although there might be some instances where you could use constants in functions

@robloxcommunityplus Really? Are you sure that you couldn’t just define a variable as

local A = 20

And not change its value?

True, but there aren’t really any true constants in Lua. (Unless you’re talking about metatables, but in that case, you can still just use rawset() unless you’re using newproxy or something)

4 Likes

As others have said, but just to be clear:

There is technically no difference in Roblox between a constant and a normal variable, but constants are just variables which shouldn’t be changed during the execution of the script.
They’re usually used as like a ‘property’ for parts of the script. E.g. in my rain module, one of the constants is RAIN_DENSITY. You’d set RAIN_DENSITY to whatever number you want, and no other part of the script changes it.

They’re usually put in CAPS to signify that they’re a constant.

7 Likes

thx so much i am so happy my constant will never change

local BEST_GAME = "AMOGN US"-- i know it should be roblox but...
1 Like

this devforum thread is making me rethink everyting :star_struck:

6 Likes

I’m sorry for bumping, but I’m going to make an essay about what are constants and the history behind them being noted with UPPER_SNAKE_CASE for future readers having this question.

What are constants?

If we see the meaning of the word “constant” itself, it means Something that does not or cannot change or vary.
How is that applicable to programming?
You see, maybe you have some variable that holds a certain value and you don’t wan’t to change it no matter what, or perhaps you don’t need to make it non-constant but the language offers optimizations for them.

Lua doesn’t support constants, and to differentiate between non-constants and constants most people use the UPPER_SNAKE_CASE naming convention.
But here’s what happens if you try reassigning a constant variable in TypeScript:

What is the story behind constants being noted with UPPER_SNAKE_CASE?

It was first used in Assembly programs when Unix was widely used (and it was considered a “lower case OS”), so the chads that programmed during those ancient times used this convention as a way to differentiate those two.
But that’s not it! Things actually really heated up with C.
So in C we have what we call preprocessors. Before compilation, the compiler will read those through and do whatever those are supposed to do.
There is this preprocessor called #define, whose job is to replace any mentions to the name given to it to the value it holds. Consider the following snippet:

#include<stdio.h>

#define someValue 1

int main() {
    printf("%d", someValue);
}

So, what happens is: the compiler will read through the file, see that we have a #define, get it’s name and value and cache it. When it sees a mention of the name, it replaces it with the value.
But, there is a catch; consider the following program:

#include<stdio.h>

#define someInt 10
struct test {
    int someInt;
    char someString[50];
}

int main() {
    struct test = {
        .someInt = someInt,
        .someString = "Great!"
    };
}

Seems normal? Yup, it does.
But you wanna know how the file looks after the compiler finishes preprocessing?

#include<stdio.h>

#define someInt 10
struct test {
    int 10;
    char someString[50];
};

int main() {
    struct test = {
        .10 = 10,
        .someString = "Great!"
    };
}

WAIT, what happened to the someInt member of the test struct???
So you see, #defines don’t really act as variables. The compiler will literally replace the name by the value no matter what the context is!
So, where does UPPER_SNAKE_CASE come in?
Well, by using BIG_SHOUTY_NAMES, the chances of accidentally reusing the name drastically decreases!

#include<stdio.h>

#define SOME_INT 10
struct test {
    int someInt;
    char someString[50];
};

int main() {
    struct test = {
        .someInt = SOME_INT,
        .someString = "Great!"
    };
}

And that sums up everything.


I hope this was an interesting read!

4 Likes