Are Enums essensially just lua tables?

Even thought it is said to be data type and is only read only.

Code-wise, enums are userdata, similar to objects such as game, workspace, workspace.Part, etc.

(I wanted to say something more but Idk how to elaborate on this)

1 Like

It’s technically userdata, just like any in-game object (e.g. a Part is userdata). It’s basically a way for C to expose data that Lua can interact with.

Internally, I believe that it does use Lua tables/metatables in order to interact with the object.

So to answer your question: Yes, kind of.

Here’s an example.

4 Likes

I remember that some of them used to be numbers a long time ago. The script would run perfectly if you replaced the enum with a number.

I’m assuming that this is what you mean by your question, so…

You can do table things (minus editing) an enum. You can do Enum.TweenStyle['Sine'] and for loops on them.

So essentially, they are just tables, but as others have pointed out they are userdata in the code

Not exactly, you can’t do things you normally would be able to with a table.

Things like
rawset(),rawget(),setmetatable(),pairs(),ipairs(),next()

You can edit the metatable on a userdata to mimic a table, but it won’t be able to do everything.
Although, userdata do have access to __len

Roblox enums use meta tables, but you can’t see them.

In short, userdatas are very different from tables.

They never have been. Sure you’re not just confusing that with the Enum’s Value?

No it wouldn’t.

image

I think he was referring to something like this

Part.Material = 288 – sets material to neon
Part.Material = “Neon” – also sets material to neon
Part.Material = Enum.Material.Neon – also sets material to neon, but is the longest

2 Likes

The Enums are actual C++ enums, defined with specific integer values. In the C++ code, there are also maps from the value to a readable string, which is what you see as the names in the drop-down menu. There is boilerplate code for converting between the enum type, its integer value and its associated string, and the Lua<->C++ reflection system does the conversion for you, which is why you can assign any of the valid variants to the property, and it lets you get the int and string with .Value and .Name respectively.

Lua tables are not involved in this process in any way; they are C++ data types using in Lua via UserData as others have mentioned above.

2 Likes

Since this is a thread about learning, let me add onto this for anyone reading: Please don’t assign using numbers! While it may work, it’s a real pain for yourself and anyone else who may have to read your code later.

For example, 288 doesn’t immediately stand out as being neon, and most people would have to do a search to find out what material 288 is. The other two options will make your life a lot easier than the first one will.

1 Like