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

String to Enum?

Asked by 9 years ago

I have a string

1("Enum.PartType.Block")

I need this string to determine the shape of a part, but It's a string so I can't say-->

1part.Shape = ("Enum.PartType.Block")

this would error.

So my question is how do you convert a string into an Enum value?

1
Why not just do part.Shape = "Block"? Perci1 4988 — 9y
1
Because the string is the return of a format JSON save which is only composed of string values. ClassicTheBlue 65 — 9y

2 answers

Log in to vote
3
Answered by
ImageLabel 1541 Moderation Voter
9 years ago

If you want to get an EnumItem of a string in the format you've provided, I can only think of looping through the members of said Enum using its GetEnumItems method, and checking for matches.

1local that = ("Enum.PartType.Block")
2 
3for i, v in next, Enum.PartType:GetEnumItems() do
4    if tostring(v) == that then
5        warn(v)
6    else
7        print(v)
8    end
9end

If you want to be able to use the actual enumerated value, you could make a function to return the EnumItem from a provided string. It would work a little like the following..

01local that = ("Enum.PartType.Block")
02local part -- define
03 
04enumFromString = function( str )
05    for i, v in next, Enum.PartType:GetEnumItems() do
06        if tostring(v) == that then
07            return v
08        end
09    end
10end
11 
12local this = enumFromString(that)
13part.Shape = this
Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
9 years ago
1local String = ("Enum.PartType.Block")
2 
3 
4String = String:gsub("Enum%.PartType%.","")
5print(String) --> "Block"

Answer this question