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

String to Enum?

Asked by 8 years ago

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?

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

2 answers

Log in to vote
3
Answered by
ImageLabel 1541 Moderation Voter
8 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.

 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
Ad
Log in to vote
0
Answered by
Validark 1580 Snack Break Moderation Voter
8 years ago
local String = ("Enum.PartType.Block")


String = String:gsub("Enum%.PartType%.","") 
print(String) --> "Block"

Answer this question