Need help making a part have a random color but with specific colors?

INFO
So i’ve been wondering if I could ever make a part have a random color but with specific colors.

ISSUE
The problem is I’ve remember doing it right but now I completely forgot how to do it. I’ve been trying some method somehow it’s kinda buggy.

ATTEMPTS
Somehow I did find some solutions on some tutorials but it didn’t match what I wanted to do.

MY CODE

local Childrens = script.Parent:GetChildren()

for i,v in pairs(Childrens) do
	wait()
	if v:IsA("MeshPart") then
		v.BrickColor = BrickColor.Random("Cocoa","Crimson")
	end
end

EXTRA INFO

I only wanted my parts to have a random color but 2 colors only. However, it didn’t work at all instead it gave my parts yellow, white, black, aqua, etc…

But I only need 2 colors for them to have random color.

Ways of making it work
Since I’ve completely forgotten how to do it right, can’t do it properly right now. I’m not asking for code, just need the right way to give it 2 random colors. Cocoa and Crimson.

I could use math.random() for this but I think it wouldn’t work either. I am just gonna look through further replies in this topic.

i think random is all lowercase, isn’t it? Or maybe I’m wrong. I tried doing this too, but it only gave me the colour I put first in the parameter

I created several parts and put them in a model then use a for loop in pairs getchildren

I would suggest creating a table of your desired colors, then do a math.random on the table within your for loop.

Edit: See nooneisback’s reply below for what I mentioned above.

local colors = {
    BrickColor.new("Cocoa"),BrickColor.new("Crimson")
}
for i,v in pairs(script.Parent:GetChildren())do
    if v:IsA("MeshPart") then
        v.BrickColor = colors[math.random(1,#colors)]
    end
end
7 Likes

To explain this: A array is created storing all or the possible colors. Then, for each part under the model the brick color property is changed randomly by using a random index of the array.

4 Likes

The problem is with the BrickColor where you didn’t add .new and with random where you can either use math.random or random.new.
Thankfully @nooneisback provided the correct way of performing that task.

3 Likes

I want it to have random color in alternate like the first part is Crimson and 2nd is Cocoa then 3rd is Crimson

If you’d rather not have it random you can simply make a variable (outside of the for loop), called local lastColor = BrickColor.new("Cocoa")

Then have an if else statement inside of the for loop.

local lastColor = "Cocoa"
for i, v in pairs(script.Parent:GetChildren()) do
    if v:IsA("MeshPart") then
        local color
        if lastColor == "Cocoa" then
            color = "Crimson"
        elseif lastColor == "Crimson" then
            color = "Cocoa"
        end
        if color ~= nil then
            v.BrickColor = BrickColor.new(color)
            lastColor = color
        end
    end
end

Note: I am on mobile and have not tested the above code.

1 Like

I would want it to have random but alternatively do random color for every parts.

The code above should alternate them I didn’t test it last night like I had wanted to. Did it yield the results you were looking for?

1 Like

Consider you have a set of yellow, green, blue.

  1. Do you want a set order like:
    yellow, green, blue, yellow, green, blue
  2. Do you want a set random order:
    blue, yellow, green, blue, yellow, green
  3. Or do you want completely random?
    blue, green, green, yellow, blue, yellow, green, green

I’m confused on what you want.

2 Likes

Just make list of rgb colors and get random color from it, then just get random r g b from

1 Like

To make this functionality you would need something like

local colors = {"Crimson", "Cocoa"}
local i = 1

local children= script.Parent:GetChildren()

for _,v in pairs(children) do
    wait()
    if v:IsA("MeshPart") then
        v.BrickColor = BrickColor.new(colors[i])
        i = i + 1
    if i >= #colors then
        i = 1
    end
end

This will alternate between Crimson and Cocoa, if this is what you are after. I don’t fully understand what you want to do.

Just like that. But only two colors. Cocoa And Crimson.

Yes! Thank so much for the replies! :smile: @Antradz @nooneisback Thank you all! :smiley: I finally got it to have the colors the right way! :grin:

It worked perfectly. Thanks so much. I appreciate you helping me.

1 Like

No problem, glad I could help you!

1 Like