- What do I want to achieve?
I have a function that creates a new part via Instance.new(). I also created a new MaterialVariant and would like to apply it to my newly created part alongside all other properties
- What is the issue?
After numerous iterations, I have received the same error time and time again:
“Unable to assign property MaterialVariant, string expected, got Instance”
Here is the first iteration of my ModuleScript:
Edit: I forgot to add it to the below scripts, but MaterialService was correctly labeled in above the function.
function TerrainGenerator.createCustomPart()
-- Create a new part
local newPart = Instance.new("Part")
-- Elaborate on newPart's properties
newPart.Size = Vector3.new(25.125, 1, 37.938)
newPart.Anchored = true
newPart.BrickColor = BrickColor.new("Brown")
-- Get newPart's BASE material
newPart.Material = Enum.Material.Ground
-- Get newPart's Material Variant
newPart.MaterialVariant = MaterialService:GetMaterialVariant(Enum.Material.Ground, "RealisticDirtPath1")
-- Parent the object to the workspace
newPart.Parent = workspace
end
This unfortunately did not work, and so I made a second iteration:
function TerrainGenerator.createCustomPart()
-- Create a new part
local newPart = Instance.new("Part")
-- Elaborate on newPart's properties
newPart.Size = Vector3.new(25.125, 1, 37.938)
newPart.Anchored = true
newPart.BrickColor = BrickColor.new("Brown")
-- Define Variables for BaseMaterial and MaterialVariant
local baseMaterial = Enum.Material.Ground
local materialVariantName = "RealisticDirtPath1"
-- Set the base material of newPart
newPart.Material = baseMaterial
-- Get newPart's MaterialVariant
local materialVariant = MaterialService:GetMaterialVariant(baseMaterial, materialVariantName)
if materialVariant then
newPart.MaterialVariant = materialVariant
else
warn("This material variant does not exist")
end
newPart.Parent = workspace
end
- What solutions have I tried thus far?
As listed above, I first tried to rewrite the code, but that did not work. I then looked at the documentation for MaterialService and GetMaterialVariant, which really did not help much. The GetMaterialVariant method only requires two arguments: The base material, and the material variant as a string.
I made sure that my custom Material Variant did exist, and all instances of the name across the ModuleScript were spelled accordingly to the variant in the object explorer.
Lastly, I tried asking the assistant, and after it verifying my script, I was told to ask the devhub forum.
Any help in this matter would be greatly appreciated, thanks in advance.