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

Is There a Way to Shorten Conditions?

Asked by 4 years ago

Is there an easy way to shorten this line of code?

if (part.Material == Enum.Material.Concrete or part.Material == Enum.Material.Slate or part.Material == Enum.Material.Cobblestone or part.Material == Enum.Material.Granite or part.Material == Enum.Material.Marble) then
0
use a table of valid materials, then loop through it theking48989987 2147 — 4y

1 answer

Log in to vote
3
Answered by 4 years ago

Yes.

Tables

Use tables. Tables have an array part and a dictionary part. The dictionary part is an unordered mapping of key-value pairs.

local materials = {
    Concrete = true,
    Slate = true,
    Cobblestone = true,
    Granite = true,
    Marble = true
}

if (materials[part.Material.Name]) then
    -- something
end

If the name of the material is a key in the table, materials[part.Material.Name] will evalĂșate to true. Otherwise nil.

Ad

Answer this question