Any way to get a material with text?

So basically I want to to get a material depending on this variable:

local material = "Sand" -- the variable

I would then want to get the actual material which is:

Enum.Material.Sand -- this is what the actual material is

So is there anyway to do something like this Ig?

local realMaterial = Enum.Material(material) -- something like this I guess.

What Im trying to do is something kind of similar to if you were trying to get a part from a folder:

local part = folder:FindFirstChild(OBJNAME) -- this would get a part depending on the name of the variable.

I want to basically do that except with getting a material,


Cool thanks!

Hey there,

Two interesting methods to achieve this.

First one is just some RegEx magic with character classes.

local function getMaterial(obj)
	if obj and obj.Material then
		return string.match(tostring(obj.Material), "%.(%w+)$")
	end
end

Second is much much simpler, and it is essentially based around treating Enums as dictionaries and directly accessing their Name property.

local function getMaterial(obj)
	if obj and obj.Material then
		return obj.Material.Name
	end
end

I’m unsure if I misunderstood you and I apologize if I did.

Hope this helps. Cheers.

1 Like
local str = "Sand"
local material = Enum.Material[str]
print(material.Value, material.Name)

Rather simple, if I understand what you’re looking for correctly.

And ZEDD’s methods to get a material from an object.

2 Likes

Well thing about this code is it uses the obj.Material.

What I want to do does not have an object.

I’m sorry if I may have partially misunderstood you, regardless, hope you still found it helpful.

And for what you’re asking, I believe Meerkat’s method does exactly that. You can always modify my functions to take a Material’s Enum value as an argument instead of an instance.

1 Like