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

Is There a Way to Shorten Conditions?

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

1 comment

0

use a table of valid materials, then loop through it

1 answer

✓ Accepted answer

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.

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