Search code examples
luascriptingrobloxluau

How to destroy parts in order by colour


While scripting a game in ROBLOX I was unable to make a script that removed a part in order by colour.

I tried:

local partColours = {'Really Red', 'Really Blue', 'Magenta', 'Lime Green'}
local folder = game.Workspace.Color:GetChildren()
for i, v in pairs(folder) do
    if v.BrickColor == partColours then
        wait(1)
        v:Destroy()

    else
        error('L')
    end
end


Solution

  • The easiest way to do those would be to loop through the folder of parts multiple times. Each time looking for the next color in the list.

    Also, to make sure you are making the right comparison, be sure to check the BrickColor's Name.

    local partColours = { 'Really red', 'Really blue', 'Magenta', 'Lime green'}
    local folder = game.Workspace.Color
    
    -- look for each colour at a time
    for _, colour in ipairs(partColours) do
        local parts = folder:GetChildren()
        for _, part in ipairs(parts) do
            -- escape if the object doesn't have a BrickColor property
            if not part:IsA("BasePart") then
                continue
            end
    
            if part.BrickColor.Name == colour then
                part:Destroy()
                wait(1)
            end
        end
    end