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
7 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.

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)

1 answer

Log in to vote
7
Answered by 7 years ago
Edited 7 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;

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.

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

Answer this question