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

How do you make a brick that rapidly changes material?

Asked by 7 years ago

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.

0
Are you saying to make it a random Material every so seconds? KingLoneCat 2642 — 7y

2 answers

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

Enums


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?

Example A

-- Setting a part's material using it's enum item
part.Material = Enum.Material.Slate

Example B

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

Selecting random materials


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.

0
You can also use string values instead of enums, but I still prefer enums. LordProxius 17 — 7y
0
I stated that under the example code. ScriptGuider 5640 — 7y
Ad
Log in to vote
0
Answered by 7 years ago

Hey Inferno_Noodle,

If by changing Material rapidly you mean selecting a random material over and over then, below what you would do in english.

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

Below is a personal example of the code that you could do with that.

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

Well, I hope I helped in one way or another and the links for the main concepts I used above will be below.

Tables

While Loops

~~ KingLoneCat

Answer this question