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

(SOLVED) Select a random material efficiently?

Asked by 4 years ago
Edited 4 years ago

Hello everyone.

My game involves selecting a random material for lots of separate objects. First, I attempted this by selecting a random material from an array, and then setting the object's material to that.

local mat = {"Plastic","SmoothPlastic","Neon","Wood","WoodPlanks","Marble","Slate","Concrete","Granite","Brick","Pebble","Cobblestone","CorrodedMetal","DiamondPlate","Foil","Metal","Grass","Sand","Fabric","Ice"}

local children = script.Parent.LeftBackLeg:GetChildren()
for i, child in ipairs(children) do
    local material = mat[math.random(0,19)]
    child.Material = Enum.Material.material
end

This gave me the error: "material is not a valid EnumItem" which I suppose makes sense because I said to set it to that? But I already selected a random material from the array which confuses me.

Is there any way to do this besides select a random number using math.random and saying if it's that number it selects a different material (extremely tedious).

Thanks! :D

1 answer

Log in to vote
1
Answered by
bluzorro 417 Moderation Voter
4 years ago

Instead of Enum.Material.material you should put Enum.Material[material]. If that doesn't work, then you can do child.Material = material because changing the material property can also be done by putting the material's name in a string.

0
I will test this out when I get to my computer. If it works I will accept it! Thanks for the good ideas man proqrammed 285 — 4y
0
no problem bluzorro 417 — 4y
0
Enum.Material[material] worked. Tysm! proqrammed 285 — 4y
Ad

Answer this question