 # How do i randomize a part material?

Title pretty much explains it all. How would get this to work?
Help would be appreciated as always

``Part.Material = math.random()``
2 Likes

Try utilize the `Enum.Material`, knowing that it is using `Enums`. Although that, all the values are ridiculously out of order.

I do not know all that much about randomizing, think you could show me an example?

This is a method that utilises Enumerations.

``````local Materials = Enum.Material:GetEnumItems() -- Get a table of all the possible materials

local RandomMaterial = Materials[math.random(#Materials)] -- Select a random value inside the table

Part.Material = RandomMaterial -- Set the random material``````
13 Likes

Oh, thats alot simpler than i thought it would be. Yeah also dont know all that much about tables too, Thanks!

``````local part = script.Parent
local partColorRandomizingTable =
{
Color3.fromRGB(0,255,0);
Color3.fromRGB(255,0,0);
Color3.fromRGB(0,0,255)
}

part.BackgroundColor3 = math.random(1, #partColorRandomizingTable)
``````

could be a more efficient way but… I would do something like this, if anyone knows a better way, please let me know too
EDIT: didn’t see the post above, my bad

https://developer.roblox.com/en-us/api-reference/enum/Material

For reference, check the link above.

@Proxus I’m skeptic about the other materials that is not applicable for `BaseParts`, because some of the materials belongs to terrain. Any ideas about the behavior?

Alternate solution, that ignores a set of “invalid” materials.

``````local Materials = Enum.Material:GetEnumItems() -- Get a table of all the possible materials
local IgnoreMaterials = {
Enum.Material.Air; Enum.Material.Water;
Enum.Material.Rock; Enum.Material.Asphalt;
Enum.Material.Snow; Enum.Material.Glacier;
Enum.Material.Sandstone; Enum.Material.Mud;
Enum.Material.Basalt; Enum.Material.Ground;
Enum.Material.CrackedLava; Enum.Material.Salt;
Enum.Material.LeafyGrass; Enum.Material.Limestone;
Enum.Material.Pavement;
} -- Materials to ignore from the list

for _, material in next, IgnoreMaterials do -- Removing unnecessary materials
table.remove(Materials, table.find(Materials, material))
end

local RandomMaterial = Materials[math.random(#Materials)] -- Select a random value inside the table

Part.Material = RandomMaterial -- Set the random material``````
8 Likes

There’s another Enum for Terrain Materials called CellMaterial, though some of those in there are shared by Enum.Materials, too (and outdated, I think?). The way it’s structured is not very helpful… Terrain materials and brick materials should be separated.

Making an ignore list as you have would work well enough, too.

1 Like

i get an error at line 14

bad argument #1 (table expected, got EnumItem)

Woops! Fixed it. Just happened to have a hiccup.

1 Like

Works much better now, Thanks for your help!

wait, How do i put in the materials into the table?
when you click its suppose to spawn a random Material.
I just cant put in the materials
where it says `Brick, Fabrick` its not working
I dont understand.

``````
game.Workspace.ButtonModel.Button.Anchored = true

local Materials = Enum.Material:GetEnumItems(Brick, Fabric) -- Get a table of all the possible materials

local RandomMaterial = Materials[math.random(Materials)] -- Select a random value inside the table

function onMouseClick()
print("You clicked me!")

local RandomNumber = math.random(1, 10)
local newPart = Instance.new("Part",game.Workspace)

newPart.Size = Vector3.new(8, 1, 8)
newPart.Position = Vector3.new(-29, 30.5, -113)
newPart.Anchored = false
newPart.Transparency = 0
newPart.CanCollide = true
newPart.Material = RandomMaterial
newPart.BrickColor = BrickColor.Random()

wait(4)

newPart:Destroy()

end

clickDetector.MouseClick:connect(onMouseClick)

is there a simpler Version?
Just trying to get answers to get better at scripting 1 Like

As explained in the comments, using the method `GetEnumItems()` on `Enum.Materials` will return a table of all possible materials, however since you cannot make a BasePart’s material a terrain material you must remove those from the table returned. Which you can see in Operatik’s post.

1 Like

OOOh, got it Thank you very Much!

1 Like

apparently it doesnt work for me.
what went wrong?

``````local mat = math.random(1,2)
local FirstGenerate = nil
local SecondGenerate = nil
local ThirdGenerate = nil

local Materials = Enum.Material:GetEnumItems()

local IgnoreMaterials = {
Enum.Material.Air; Enum.Material.Water;
Enum.Material.Rock; Enum.Material.Asphalt;
Enum.Material.Snow; Enum.Material.Glacier;
Enum.Material.Sandstone; Enum.Material.Mud;
Enum.Material.Basalt; Enum.Material.Ground;
Enum.Material.CrackedLava; Enum.Material.Salt;
Enum.Material.LeafyGrass; Enum.Material.Limestone;
Enum.Material.Pavement
} -- Materials to ignore from the list

for _, material in next, IgnoreMaterials do -- Removing unnecessary materials
table.remove(Materials, table.find(Materials, material))
end

SecondGenerate = Materials[math.random(#Materials)]
ThirdGenerate = Materials[math.random(#Materials)]
FirstGenerate = Materials[math.random(#Materials)]

local rand = Random.new()

local function randomColor()
return Color3.new(rand:NextNumber(), rand:NextNumber(),
rand:NextNumber())
end

print(FirstGenerate)
print(SecondGenerate)
print(ThirdGenerate)

for x = -1000, 1000 do
for z = -1000, 1000 do

local noise = math.noise(seed, x/70, z/70) * 50
if noise < 1 then
workspace.Terrain:FillBall(Vector3.new(x,noise,z),2,FirstGenerate)
end

if noise > 1 and noise < 2 then
workspace.Terrain:FillBall(Vector3.new(x,noise,z),2,SecondGenerate)

end
if noise > 5 then
workspace.Terrain:FillBall(Vector3.new(x,noise,z),2,ThirdGenerate)
end
local mathrandom2 = math.random(1,1000)

if mathrandom2 == 5 then
local pie = script.Coral:Clone()
pie.Anchored = true
pie.Color = Color3.new(math.random(1,255),math.random(1,255),math.random(1,255))
pie.Parent = workspace.Things
pie.Position = Vector3.new(x,noise,z) + Vector3.new(0, 6, 0)

end
local mathrandom3 = math.random(1,10000)
if mathrandom3 == 5 then
local pie = script.Statue:Clone()
pie.Anchored = true
pie.Parent = workspace.Things
pie.Position = Vector3.new(x,noise,z) + Vector3.new(0, 4, 0)

end
local mathrandom4 = math.random(1,10000)
if mathrandom4 == 5 then
local pie = script.Rock:Clone()
pie.Anchored = true
pie.Parent = workspace.Things
pie.Position = Vector3.new(x,noise,z) + Vector3.new(0, 4, 0)
end
local mathrandom5 = math.random(1,5000)
if mathrandom5 == 5 then
local pie = script.Reef:Clone()
pie.Parent = workspace.Things
pie.Root.CFrame = CFrame.new(x,noise,z) * CFrame.new(0, 3, 0)
end
local mathrandom6 = math.random(1,10000)
if mathrandom6 == 5 then
local pie = script.Arch:Clone()
pie.Parent = workspace.Things
pie.Position = Vector3.new(x,noise,z) + Vector3.new(0, 2, 0)
pie.Orientation = Vector3.new(pie.Orientation.X,math.random(1,360),pie.Orientation.Z)
end

end
end``````

Either high probability of new materials being added into it(it isn’t a permanent solution, because it is still not easy to distinguish terrain materials from `BasePart`-compatible ones) or something else.

1 Like