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

Setting string value to material type?

Asked by
NotSoNorm 777 Moderation Voter
9 years ago

So I have this string value that changes to a material (Plastic,Fabric,Neon,etc...) How would I set a material to that string value?

here is a little line or so

local material = script.Parent.Parent.MaterialPicker.MainFrame.CurrentMaterial.Value
slab.Material = Enum.Material.material --Obvi not working

2 answers

Log in to vote
2
Answered by 9 years ago

You must use []'s when specifying a variable as the index, so it would be slab.Material = Enum.Material[material]

Ad
Log in to vote
2
Answered by
dyler3 1510 Moderation Voter
9 years ago

A simple way to do this, is simply set it to the string. You almost had it down, but in this case Enum.Material.(string), would not work. So, there is two different methods you can use. An example of what I mean is like this:

material="Plastic"
game.Workspace.Part.Material=material --Works :P

--or

material="Plastic"
game.Workspace.Part.Material=Enum.Material[material]

So to apply this to your script, we'll use the same variable and everything, and simply change it so that it sets the Material equal to the string:

local material = script.Parent.Parent.MaterialPicker.MainFrame.CurrentMaterial.Value
slab.Material = material

Now we'll try it using the second method:

local material = script.Parent.Parent.MaterialPicker.MainFrame.CurrentMaterial.Value
slab.Material =Enum.Material[material]

Either of these would work correctly, you can decide which one to use. For simplicity, I would go with the first method however.


Anyways, hope this helped. If you have any further problems/questions, please leave a comment below, and I'll see what I can do :P

-Dyler3

Answer this question