Check if an enum exists?

So, how would i check if an enum exists?
I have tried:

local FontT = "Arial"
if Enum.Font:GetEnumItems()[FontT] ~= nil then
print("works")
else
print("doesn't work")
end

This always prints “doesn’t work”
I’ve also tried

local FontT = "Arial"
if Enum.Font[FontT] ~= nil then
print("works")
else
print("doesn't work")
end

Now, at first this works, but if we replace “Arial” with something that doesnt exist it errors for some reason.
Any help is appreciated :heart:

You can try using this function that i made, Then all you need to do is to write the first parameter (EnumList) as a string. EnumList is Enums like Font, FontSize, HumanoidRigType, etc. EnumChild is the Child which you want to check if it exists.

local function DoesEnumExists(EnumList: string, EnumChild: string): boolean
	return pcall(function()
		return Enum[EnumList][EnumChild] ~= nil
	end)
end

if DoesEnumExists("Font", "Arial") then
	print("Arial Font Exists!")
end
3 Likes

Ah, how could I not think of using pcalls! Thanks!

1 Like

I’d avoid using pcall() when there are other alternatives as it’s not very performant. Here’s a function which takes two parameters, the first of which represents an enum list & the second of which represents an enum item.

local function doesEnumExist(enumList : string, enumItem : string) : boolean
	for _, _enumList in ipairs(Enum:GetEnums()) do
		if tostring(_enumList) == enumList then
			for _, _enumItem in ipairs(_enumList:GetEnumItems()) do
				if _enumItem.Name == enumItem then
					return true
				end
			end
		end
	end
	return false
end

print(doesEnumExist("PartType", "Block")) --true
print(doesEnumExist(math.huge, math.pi)) --false

https://developer.roblox.com/en-us/api-reference/datatype/Enums
https://developer.roblox.com/en-us/api-reference/datatype/Enum

You can find documentation for GetEnums() and GetEnumItems() here, respectively.

3 Likes

This one seems to work pretty well too, thanks! :heart: