Printing the material of a part?

How come print(part.Material) doesn’t work

2 Likes

It does work, as long as part variable actually points to a part. A generic “it doesn’t work” with no other context isn’t helpful either

1 Like

Could you provide more code, your explorer tree, etc? Simply stating “print(part.Material)” doesn’t help us answer your question. What part are you trying to print the material of, where is that part located, etc?

print(part.Material) certainly will work if done correctly.

The parent is irrelevant here, it’s what part is referencing. This thread’s vagueness doesn’t point adequately to the problem.

3 Likes

Did you assign the part as a variable already? More code would help.

We need more information to give the best approach to the problem.
Though maybe tostring() it (Sometimes helps).

Using tostring(part.Material) will turn the Enum into string. But it will also include the Enum.Materials. String manipulation must be used.

I’ve just scripted this really quickly. It uses string.sub to get the position of the 2 dots, and then uses string.sub again to get the material as string. Hopefully this helps.

local function FindCharacterInString(String, CharacterToFind, Start, End)
    if not String or not CharacterToFind or not Start then return end
    if not End then End = string.len(String) end
    local CharacterPos
    for i = Start, End do
	    if string.sub(String, i, i) == CharacterToFind then
	    	CharacterPos = i
	    	break
	    end
    end
    return CharacterPos
end

local function GetMaterialOfPartAsString(Part)
    if not Part then return end
    local EnumAsString = tostring(Part.Material)
    local FirstDotPos = FindCharacterInString(EnumAsString, ".", 1)
    if FirstDotPos then
    	local SecondDotPos = FindCharacterInString(EnumAsString, ".", FirstDotPos + 1)
	    if SecondDotPos then
	    	local MaterialOfPart = string.sub(EnumAsString, SecondDotPos + 1)
		    return MaterialOfPart
	    end
    end
end

local NewPart = Instance.new("Part", workspace)
NewPart.Material = Enum.Material.Neon
print(GetMaterialOfPartAsString(NewPart)) -- Neon
game:GetService("Debris"):AddItem(NewPart, 1)
2 Likes

A better option would just to use tostring(material):match("%.(%w+)$") but I don’t think that’s what this question is asking anyways.

3 Likes

He’s asking about how to print the material of a part, my script does exactly that.

Does it print a wrong result or doesn’t print at all?
If it prints something like Enum.Materials.Plastic for example, you can always do print(part.Material.Name) to print out the actual name of the material, or in this example, Plastic

If it doesn’t print at all, can you give us the output or the script, so we know why it doesn’t work?

14 Likes