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

How to change a model's color using :GetChildren() and another table?

Asked by 6 years ago

I'm pretty new at coding. I've been using this ROBLOX game to help me sharpen my skills. This game teaches the basics of Lua and then provides some projects for you to complete. I'm currently stuck on one of these projects.

The project wants you to: Make a loop that changes all the colors in a model to one color every few seconds. Put the list of colors in a table, and have it cycle through the colors in the same order.When it reaches the last color, it will restart and cycle through again. Get all the children from the model with the GetChildren() method and iterate through them with a for loop.

I've tried to complete this project myself but keep failing. The best result I've got is getting a single part to change color and then another part to change color and so on and so on until the parts have cycled through the table of colors.

sp = script.Parent

function LIGHT_IT_UP()
    local c = {BrickColor.Red(), BrickColor.Blue(), BrickColor.Green(), BrickColor.Yellow()
    }
    local m = sp:GetChildren()

    while true do
        for t = 1, #c do
            for i,v in pairs(m) do
                if v:IsA("Part") then
                    v.BrickColor = (c[t])
                    wait(1)
                end
            end
        end
    end
end

game.Players.PlayedAdded:connect(LIGHT_IT_UP)

Hope you lads can help me out.

1
You are waiting per part not per colour. User#5423 17 — 6y
0
You're right, thanks! TheLegendKaiba 0 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago

Wait(1) makes your code wait 1 second after one part's color has been changed

while true do
    for t = 1, #c do -- iterates through all colors in table
        for i,v in pairs(m) do -- iterates through all parts >one by one<
            if v:IsA("Part") then
                v.BrickColor = (c[t]) -- changes color of 1 part
                wait(1) -- after changing color code waits 1 second and goes to start (iteration through parts)
            end
        end
    end
end

You should move wait(1) to the next line after the iteration through all parts:

while true do
    for t = 1, #c do -- iterates through all colors in table
        for i,v in pairs(m) do -- iterates through all parts >one by one<
            if v:IsA("Part") then
                v.BrickColor = (c[t]) -- changes color of 1 part and instantly goes to start (iteration through parts)
            end
        end
        wait(1) --after all part's colors have been changed waits 1 second and then goes to start (iteration through colors)
    end
end
0
Thanks, mate. TheLegendKaiba 0 — 6y
Ad

Answer this question