Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

(SOLVED) Select a random material efficiently?

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

✓ Accepted answer

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.

3 comments

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

0

no problem

0

Enum.Material[material] worked. Tysm!

This discussion is preserved as it was. New answers, comments, and votes are closed.