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-->
1 | part.Shape = ( "Enum.PartType.Block" ) |
this would error.
So my question is how do you convert a string into an Enum value?
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.
1 | local that = ( "Enum.PartType.Block" ) |
2 |
3 | for i, v in next , Enum.PartType:GetEnumItems() do |
4 | if tostring (v) = = that then |
5 | warn(v) |
6 | else |
7 | print (v) |
8 | end |
9 | end |
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..
01 | local that = ( "Enum.PartType.Block" ) |
02 | local part -- define |
03 |
04 | enumFromString = 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 |
10 | end |
11 |
12 | local this = enumFromString(that) |
13 | part.Shape = this |
1 | local String = ( "Enum.PartType.Block" ) |
2 |
3 |
4 | String = String:gsub( "Enum%.PartType%." , "" ) |
5 | print (String) --> "Block" |