Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

String to Enum?

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?

2 comments

1

Why not just do part.Shape = "Block"?

1

Because the string is the return of a format JSON save which is only composed of string values.

2 answers

✓ Accepted answer

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"

This discussion is preserved as it was. New answers, comments, and votes are closed.