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

Random Skin color on join is not working?

Asked by 7 years ago
colors={"Brick yellow", "Light orange", "Nougat", "Bright orange"}
Color=BrickColor.new(colors[math.random(1,6)])
for i,v in pairs(script.Parent:GetChildren()) do
    if v.Name=='Head' or v.Name=='Right Leg' or v.Name=='Right Arm' or v.Name=='Left Arm' or v.Name=='Left Leg' or v.Name=='Torso' then
        v.BrickColor=Color
    end
end

Is there something missing or something completely wrong?

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

You're doing a couple things wrong. First of all, when you use math.random(), you should call math.randomseed(tick()) to make sure you get random value. Plus, you are searching for a random chance of 1-6 values when you only have 4 values inside of your color table, a good way to always get the same amount of values in your table is to use the # which will give the amount of entries in said table (4).

Not only that, because you're using Strings to get your colors, you need to use BrickColor = BrickColor.new(String),the proper way to change BrickColors.

Here is a fixed version of your script following my above instructions:

math.randomseed(tick())
colors = {"Brick yellow", "Light orange", "Nougat", "Bright orange"}
Color = BrickColor.new(colors[math.random(1, #colors)])
for i,v in pairs(script.Parent:GetChildren()) do
    if v:IsA("BasePart") then
        v.BrickColor = BrickColor.new(Color)
    end
end 

You notice I did not look for names of body parts, but instead I'm seeing if an instance inside the character model (Which should be script.Parent) is classified as "BasePart", Hence :IsA(), which checks for class of an instance. If you look through a character model, you'll notice only the body parts are actually classified as a BasePart (This includes HumanoidRootPart which you can't see, but this doesn't matter), because of this, you can type a lot less while having this script work for R6 and R15 character models.

Hope this helps!

0
You should use a simple for loop on arrays like those returned by GetChildren(), and it's unnecessary to have [`1] as a parameter in math.random. Validark 1580 — 7y
Ad

Answer this question