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

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

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

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.

6 comments

0

Right away I get "Materials is not a valid EnumItem" in output.

0

On line 3

0

Oops, my mistake, it's Material, not Materials.

0

I get "Players.Player1.PlayerGui.Main.BottomContainer.Buttons.HomeMaterial.LocalScript:6: bad argument #1 to 'pairs' (table expected, got Enum)"

0

Replace local oMaterials = Enum.Material with oMaterials = Enum.Material:GetEnumItems().

0

Ah, is that the problem? Enums are their own special value type?

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