I have a string
("Enum.PartType.Block")
I need this string to determine the shape of a part, but It's a string so I can't say-->
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.
local that = ("Enum.PartType.Block") for i, v in next, Enum.PartType:GetEnumItems() do if tostring(v) == that then warn(v) else print(v) end 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..
local that = ("Enum.PartType.Block") local part -- define enumFromString = function( str ) for i, v in next, Enum.PartType:GetEnumItems() do if tostring(v) == that then return v end end end local this = enumFromString(that) part.Shape = this
local String = ("Enum.PartType.Block") String = String:gsub("Enum%.PartType%.","") print(String) --> "Block"