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 4 years ago
Edited 4 years ago

I am trying to assign something a material based on a string value. Here is the code:

1local Mat = script.Parent:WaitForChild("Material").Value
2local Col = script.Parent:WaitForChild("Color").Value
3 
4while true do
5    Part.Material = Material.new(Mat)
6    Part.BrickColor = BrickColor.new(Col)
7end

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
4 years ago
Edited 4 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:

1BasePart.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:

01--###----------[[VARIABLES]]----------###--
02local DropperModel = script.Parent
03 
04local DropPartTemplate = DropperModel.Part
05 
06local DropMaterial = DropperModel.DropMaterial
07local DropColor = DropperModel.DropColor
08 
09 
10 
11--###----------[[LOGIC]]----------###--
12DropMaterial:GetPropertyChangedSignal("Value"):Connect(function()
13    DropPartTemplate.Material = Enum.Material[DropMaterial.Value]
14end)
15 
16DropColor:GetPropertyChangedSignal("Value"):Connect(function()
17    DropPartTemplate.BrickColor = BrickColor.new(DropColor.Value)
18end)
Ad
Log in to vote
0
Answered by 4 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 — 4y
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 — 4y
0
oh, I don't think that Material.new is a thing. OisinBearie3 0 — 4y

Answer this question