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

How do I set certain colors within a random script?

I have a humanoid, animal model that I want to spawn in with different colors. I'm able to do this with:

script.Parent.Part.BrickColor = BrickColor.Random()

but have not figured out quite yet how to set certain colors so this model doesn't spawn in bright pink or blue.

I'm also wondering how I would have multiple parts become the same random color if that's possible.
I have these parts: Head, Torso, Right Arm, Left Arm, Right Leg, Left Leg, Tail, Snout, Right Ear, and Left Ear. Nose, Left Eye, and Right Eye are welded on.

I want to have the ears and snout become one random color and the rest as one random color.

Any answers are appreciated ahead of time!

3 answers

✓ Accepted answer

OK, before I answered your question I wrote another one and realized it wasn't going to satisfy what you were looking for D:

But, I have revised it.

What you could do is group the ears and snout into one model and group the rest into another one.

Now, add a brick into each model and set their name to 'COLOR' or something.

Make them invisible so they aren't, well, visible.

Put this code into each one:

--This will set a variable for your random color
local color = script.Parent.BrickColor

color = BrickColor.Random()

--Now, we will use a 'pairs' loop to gather everything in the model

for _,v in pairs (script.Parent.Parent:GetDescendants()) do
	if v.ClassName == "Part"  then --you can add 'or' if you need multiple class names found
	v.BrickColor = color
end

Hope this helps! It worked for me at least when I tested it

so for your first question:

u make an array with all the random colors u want to choose from then u pick a random number between that array size and use that to pick the random color from your array of colors and

local colors ={} -- filll the array with colors u want

local randomNumber = Random:NextInteger ( 1, #colors )
script.Parent.Part.BrickColor = colors[randomNumber]

for your second question:

get two random colors-> make sure the both arent the same color next use the first random color to set the ears and snout and then other color to set the remaining parts

You can save the random BrickColor to a variable. If you use the variable to set the color, the Colors will be the same.


local RandomBrickColor = BrickColor.Random()
game.Workspace.Part1.BrickColor = RandomBrickColor
game.Workspace.Part2.BrickColor = RandomBrickColor

If the random color is blue, both part1 and part2 should be set to blue.

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