I know how to make a rapidly changing color brick. However, I don't know how to make a changing material brick. As you can see, there is no material script thingy like the Color3.new. I want to know if it is similar and if it is that complicated.
The Material
property (like many others) belongs to a separate data type called Enum
(short for enumeration). It consists of arbitrary keys (or enum items) that represent corresponding data, and ultimately the ID of said data. This exists so that programmers have a more readable interface when working with these values, rather than just typing in the numeric ID not knowing what it represents. For example, which of the following do you think has a clearer intent?
-- Setting a part's material using it's enum item part.Material = Enum.Material.Slate
-- Setting a part's material using it's enum item's value (same function, a lot less easier to interpret) part.Material = 800
Both examples work, but if you ask me, setting the material's value to something that ends in the name of the material is a lot easier to work with than an arbitrary number. There's also the ability to set enum properties directly to a string value, which is probably what you're most familiar with. They all work the same way, using enums.
Most people think you need to manually write out a list of existing materials, and then randomly index that list. But there's actually an easier way using enums! The method GetEnumItems
returns a list of all the elements in an enum object for you, then all that's left is choosing the random index.
-- Get a list of all materials local materials = Enum.Material:GetEnumItems() -- Set the part's material to the value of a random index from the list above part.Material = materials[math.random(#materials)]
Simple as that. If you wanted to implement this in something that continuously changes material, you'd just use a loop.
local materials = Enum.Material:GetEnumItems() local part = Instance.new("Part") part.Anchored = true part.Parent = workspace while true do part.Material = materials[math.random(#materials)] wait(1) end
That's about it. If you have any questions, just let me know. Hope it helped.
Hey Inferno_Noodle,
--[[ Make a table with all of the materials in it. Make a random number from 1 - the amount of materials in the table. Set a variable for that material which you would fetch with the random number by indexing through the table. Then, simply set that material equal to the part's material. --]]
local part = script.Parent; local materials = { Enum.Material.Plastic, Enum.Material.Slate, Enum.Material.Metal } while wait() do local random_number = math.random(1, #materials); part.Material = materials[random_number]; end
~~ KingLoneCat