Constants in lua?

You could also overwrite the global rawset function with a blank function, assuming you don’t need to use it anywhere else in your code.

anyways i tested it it works but anyways (module)

local constant = {}

local data = {}

local onSet = function(index, value)
	if data[index] ~= nil then return data[index] end
	data[index] = value
	return value
end

local metatable = {
	__metatable = true;
	
	__index = function(_, index)
		for i, v in pairs(data) do
			if i == index then
				return v
			end
		end
	end;
	
	__call = function(_, index, value)
		return onSet(index, value)
	end;
	
	__newindex = function(_, index, value)
		return onSet(index, value)
	end
}

setmetatable(constant, metatable)


return constant

Example:

local constant = require(game.ReplicatedStorage.Constants:Clone())

local runService = constant("RunService", game:GetService("RunService"))

print(constant.RunService:IsClient())

–This isn’t really useful for what you wanna achieve, but I don’t know how what you wanted to achieve could be useful in Roblox specifically /shrug

1 Like

It doesn’t have syntax for it, but Luau does constant elimination for never-set locals and upvalues so there is practically no difference.

Just use local YOUR_CONSTANT = whatever.