Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Enum over String (when possible)?

Asked by
LuaQuest 450 Moderation Voter
8 years ago

So, some people have told me that some object properties use a string value, some use Enum, and some can use both. But they can't stress it enough that if they have the option of Enum, that i use it over a string. Is this true? They both work the same, so i can't see the difference.

For example:

-- Assuming this is in a text label

script.Parent.Font = Enum.Font.ArialBold

-- Instead of ...

script.Parent.Font = 'ArialBold'

So, it's a little blurry to me since both of these methods work the same, but using a string is just generally shorter. Any good reasons?

3 answers

Log in to vote
2
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
8 years ago

When setting you can use either string, Enum, or number.

  • Number is bad, because it's not human readable. Always prefer string/enum to number.

  • String is human readable, but you could have made a typo.

  • Enum is human readable, and should prevent typos. However, it's longer.


However, when you get a value, you "have" to use Enum.

if part.TopSurface == "Smooth" then

That will not work. That's because == first checks that the two things are the same kind of thing. But "Smooth" is a string and TopSurface will be an Enum, so that comparison will always be false.

In that case, you have to use Enums.

0
Thanks for the explanation. LuaQuest 450 — 8y
Ad
Log in to vote
0
Answered by
Spectrobz 140
8 years ago

The choice is up to you! The two options are the same.

I personally use strings, since I know them perfectly.

Log in to vote
0
Answered by 8 years ago

One advantage of using 'Enum' is that, if you're typing in Roblox Studio's script editor (or command bar), its intellisense will automatically show you valid properties of "Enum" and then of "Enum.Font"

Answer this question