Colour of head isn't changing

I’m trying to get a head to change to the player’s Body Colors.HeadColor via BrickColor, and if bodyColors isn’t found, then pick a random skin tone from the table. I’ve made sure BodyColors is a member of the character, but it still generates a random skin tone from the table.

...
    if bodyColors then
        astroHead.BrickColor = BrickColor[bodyColors.HeadColor]
    else
...

And bodyColors is defined at the top of the script

local bodyColors = Character:FindFirstChild("Body Colors", true)

Bigger picture (not the whole script obviously)

local bodyColors = Character:FindFirstChild("Body Colors", true)
local realisticSkinTones = {
    Color3.fromRGB(86, 66, 54),
    Color3.fromRGB(204, 142, 105),
    Color3.fromRGB(53, 40, 33),
    Color3.fromRGB(171, 118, 88)
}

...

    if bodyColors then
        astroHead.BrickColor = BrickColor[bodyColors.HeadColor]
    else
        astroHead.Color = realisticSkinTones[math.random(1, #realisticSkinTones)]
    end

Is this on an actual humanoid?

Elaborate by what you mean “is this on” please

What error with this are you getting?

Are you trying to change the color of the head of a character that has a humanoid

No errors are being returned, although I did used to recieve attempt to index nil with HeadColor, until I changed local bodyColors = player:FindFirstChild("Body Colors", true) to local bodyColors = Character:FindFirstChild("Body Colors", true)

The target object is a head, with a humanoid sibling-ed to it, yes.

This should be changed to:

astroHead.BrickColor = bodyColors.HeadColor

since HeadColor is already a BrickColor type. It’s not something you should be trying to use as an index.

That’s like doing

astroHead.BrickColor = Nougat

Which won’t work.
On the other hand, doing

astroHead.BrickColor = BrickColor[bodyColors.HeadColor]

Is saying

astroHead.BrickColor = BrickColor.Nougat

Which works should work

No, HeadColor is a BrickColor type. It is not a string. I assure you that it’s precisely the type you want.

image

I’ve tested this script in studio and it works as I’d expect:

local colorChangingPart = workspace.ColorPart
local pl = game:GetService("Players").LocalPlayer
local ch = pl.Character or pl.CharacterAdded:Wait()
local bodyColors = ch:WaitForChild("Body Colors")

colorChangingPart.BrickColor = bodyColors.HeadColor

image

If this still isn’t working for you, you need to add some print statements and tell us the output. I’m not sure how to help at this point based on the info I have. Maybe it’d be better to provide the full script.

Problem solved by @DerryOrigin via Discord.