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

Trying to make a part become random materials when clicking a text button?

Asked by
FiredDusk 1466 Moderation Voter
8 years ago

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.

01local player = game.Players.LocalPlayer
02 
03local Materials = {'Brick', 'Concrete', 'Cobblestone'} --Table of Part Materials
04 
05script.Parent.MouseButton1Click:connect(function()
06    local PlayersHome = player.PlayersHome
07    if PlayersHome.Value ~= nil then       
08        local Walls = player.PlayersHome.Value.House.Walls
09 
10        for i,v in pairs(Walls:GetChildren()) do
11            if v:IsA('UnionOperation') or v:IsA('Part') then
12                local selectedstring = Materials[math.random(1,#Materials)] --Here is the main code I am looking for.
13                v.Material = Enum.Material.selectedstring          
14            end
15        end
16    else
17        script.Parent.Parent.Parent.Parent.FailFolder.Fail2.Visible = true
18    end
19end)

1 answer

Log in to vote
7
Answered by 8 years ago
Edited 8 years ago

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;

1local 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;

1local oMaterials = Enum.Material
2local materials  = {}
3 
4for _,v in pairs (oMaterials) do
5    table.insert(materials,v)
6end

As well as that, when searching for a child of a table, do this:

1local cName = "val1"
2tab[cName]
3 
4-- not:
5tab.cName

01local player = game.Players.LocalPlayer
02 
03local oMaterials = Enum.Materials
04local Materials  = {}
05 
06for _,v in pairs (oMaterials) do
07    table.insert(Materials,v)
08end
09 
10script.Parent.MouseButton1Click:connect(function()
11    local PlayersHome = player.PlayersHome
12    if PlayersHome.Value ~= nil then       
13        local Walls = player.PlayersHome.Value.House.Walls
14 
15        for i,v in pairs(Walls:GetChildren()) do
View all 23 lines...

Hope I helped!

~TDP

P.S: Use tab, not space.

0
Right away I get "Materials is not a valid EnumItem" in output. FiredDusk 1466 — 8y
0
On line 3 FiredDusk 1466 — 8y
0
Oops, my mistake, it's Material, not Materials. TheDeadlyPanther 2460 — 8y
0
I get "Players.Player1.PlayerGui.Main.BottomContainer.Buttons.HomeMaterial.LocalScript:6: bad argument #1 to 'pairs' (table expected, got Enum)" FiredDusk 1466 — 8y
View all comments (2 more)
0
Replace `local oMaterials = Enum.Material` with `oMaterials = Enum.Material:GetEnumItems()`. Link150 1355 — 8y
0
Ah, is that the problem? Enums are their own special value type? TheDeadlyPanther 2460 — 8y
Ad

Answer this question