I am trying to assign something a material based on a string value. Here is the code:
local Mat = script.Parent:WaitForChild("Material").Value local Col = script.Parent:WaitForChild("Color").Value while true do Part.Material = Material.new(Mat) Part.BrickColor = BrickColor.new(Col) end
I don't understand why it doesn't work because the BrickColor does but it doesn't understand when I do Material.new. Any help is appreciated.
By the way I have part as local and it's working, but it gives me the error for Material.new
BrickColor is an Object since it contains methods, attributes, and more. Whereas Material is no more than a textual identifier that describes a to-be-used texture. It is not an Object, therefore lacks a constructor function.
By taking a quick look at the documentation of Material
, you'd notice it's an Enum
. You can use that to appropriately assign the property:
BasePart.Material = Enum.Material.--[[Material Name]]
One last thing to mention: your loop has no yield, which, if you're fortunate, will cause a script-timeout. Make sure to include a wait()
somewhere in its scope.
You allocated the StringValue
s' values, which will actually store the state at which it was at runtime. You aren't creating a pointer to the property, nor can you ever. You'll need to index .Value
every time you wish to see its current state.
There is a much more efficient way of updating your property. Use the :GetPropertyChangedSignal() method of the Instance base-class. This will generate an RBXScriptSignal that you may bind a Callback to upon the suggested property's adjustment:
--###----------[[VARIABLES]]----------###-- local DropperModel = script.Parent local DropPartTemplate = DropperModel.Part local DropMaterial = DropperModel.DropMaterial local DropColor = DropperModel.DropColor --###----------[[LOGIC]]----------###-- DropMaterial:GetPropertyChangedSignal("Value"):Connect(function() DropPartTemplate.Material = Enum.Material[DropMaterial.Value] end) DropColor:GetPropertyChangedSignal("Value"):Connect(function() DropPartTemplate.BrickColor = BrickColor.new(DropColor.Value) end)
You can give a part a specific material under the code GUI, but if you want something that isn't already there, I won't be able to tell ya.