Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I assign a material?

Asked by 3 years ago
Edited 3 years ago

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

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
3 years ago
Edited 3 years ago

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.


NOTE:

You allocated the StringValues' 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)
Ad
Log in to vote
0
Answered by 3 years ago

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.

0
*colour OisinBearie3 0 — 3y
0
I'm not trying to do something not there I just wanted to make it so I could change a String Value attached to the dropper and it would change the material. odieosus 8 — 3y
0
oh, I don't think that Material.new is a thing. OisinBearie3 0 — 3y

Answer this question