There is no such thing as 'Material.random', kinda how there is 'BrickColor.random'. But I am trying to make a script when I click a TextButton, a part will turn a random material that is in the table.
local player = game.Players.LocalPlayer local Materials = {'Brick', 'Concrete', 'Cobblestone'} --Table of Part Materials script.Parent.MouseButton1Click:connect(function() local PlayersHome = player.PlayersHome if PlayersHome.Value ~= nil then local Walls = player.PlayersHome.Value.House.Walls for i,v in pairs(Walls:GetChildren()) do if v:IsA('UnionOperation') or v:IsA('Part') then local selectedstring = Materials[math.random(1,#Materials)] --Here is the main code I am looking for. v.Material = Enum.Material.selectedstring end end else script.Parent.Parent.Parent.Parent.FailFolder.Fail2.Visible = true end end)
As you should know, Materials are now accessed through Enum.Material
. And, since Enum is not an actual object, that means Enum (and it's children) are tables.
To sum that up, you don't need to define a materials table, you can just use the Enum;
local materials = Enum.Material
Now, here's the main problem (which is, luckily, easily fixed): The Enum tables are dictionaries, meaning we can't use a math.random
. Instead, we will convert the dictionary to an array;
local oMaterials = Enum.Material local materials = {} for _,v in pairs (oMaterials) do table.insert(materials,v) end
As well as that, when searching for a child of a table, do this:
local cName = "val1" tab[cName] -- not: tab.cName
local player = game.Players.LocalPlayer local oMaterials = Enum.Materials local Materials = {} for _,v in pairs (oMaterials) do table.insert(Materials,v) end script.Parent.MouseButton1Click:connect(function() local PlayersHome = player.PlayersHome if PlayersHome.Value ~= nil then local Walls = player.PlayersHome.Value.House.Walls for i,v in pairs(Walls:GetChildren()) do if v:IsA('UnionOperation') or v:IsA('Part') then v.Material = Materials[math.random(1,#Materials)] end end else script.Parent.Parent.Parent.Parent.FailFolder.Fail2.Visible = true end end)
Hope I helped!
~TDP
P.S: Use tab, not space.