Constants in lua?

Does the Lua VM support constants? I ask this as i’d rather have the clients code break than having to do this

if constant ~= value then
 error()
stateCode = false
end

Like is it something simple like in C with

#define constant    69420;
2 Likes

For constants like in C’s #define, I don’t think so. Though why do you need to check if constant ~= value?

Lua 5.4 has constants (but not in a C’s #define way) in a syntax of local x <const> = value, but that’s not available on Luau.

1 Like

Roblox is on the 5.1 architecture, right?

You can’t really define variables as constants in lua, but you can store them in a table whose metatable prevents you from modifying them.

local constants = setmetatable({}, {
	__index = {
		x = "hello"
	},
	__newindex = function() end
})

print(constants.x) -- hello

constants.x = "hi"

print(constants.x) -- hello
1 Like

couldnt i make the argument to both of you that the exploiter can call its rawset and modify it there? It seems to not work when trying on the lua demo

If you’re worried about exploiters then you should just make sure that any important code is run on the server. Players will have no control over the environment of server scripts.

3 Likes

Well now your getting off topic :confused:

You brought it up.

Besides @Blockzez answered your question in the first reply.

But literally bro it aint a true constant it can be set via the rawset function

It might be the closest thing we can have. The version of Lua that Roblox uses doesn’t support constants as far as I know. You can overwrite the rawset function too but anyone who can edit the script can just change it.

I’m not sure about constants in C, but if you want to make a value that can be access by all scripts, then you could use _G table, for example:

  • Script One
_G.MyValue = "Hello World!"
  • Script Two
print(_G.MyValue)

This will print Hello World! considering that script one ran before script two and they are both from one side (Either client or server)

global tables arent constants ;/

Hm, I’m confused, what are constants?

Variables that can’t be changed or throw an error if they’re changed

Lol I thought they were like variables that WERE changeable, anyways you can use metatables to do that; or just not change it

Like what was already mentioned multiple times in the thread that method is obsolete because you could simply change its meta data with the rawset method

To summarize what everyone else already said:

  • Lua 5.4 has a form of constants that uses the syntax local name <const> = value
  • Roblox uses their own version of Lua called Luau
  • Luau does not have any type of constants
  • You can try to simulate constants using tables depending on why you need constants

The question is why do you want constants?

And by the way, you can redefine macros in C. So if that’s your standard, it shouldn’t bother you that you can use rawset in Birdelther’s implementation of “constants”

3 Likes

Sorry im new to C, with that being i’m sure there is a way :cry:

What if I made it a module? :thinking:

You could look into using newproxy() to create a userdata, and apply metatable to that, since you can’t rawset or rawget on a userdata. But that seems like overkill, if someone uses rawset they are ruining it for themselves so shouldn’t be a big deal if they do.

5 Likes