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.
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
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.
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.
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.
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.