So essentially for my game, there’s a character customization GUI. The player has the option to select different hairs for their avatar. Upon joining, the player has a folder created inside their head called “Hair”. In this folder the MeshPart (titled Hair1, Hair2, etc.) is placed in the folder and added to the avatar. This works flawlessly, the problem I encountered is with the hair color changing. Upon clicking on the color, it fires an event titled “HairColorEvent(Number)” which corresponds to a small sequence in the central script to change the color of the hair.
This is where I ran into some problems.
The sequence is not changing the color of the player’s hair. This is the sequence:
game.ReplicatedStorage.Events.HairColorEvent1.OnServerEvent:Connect(function(plr)
local char = plr.Character
if char:findFirstChild("Humanoid") then
local HairPart = char.Head.Hair:GetChildren()
HairPart[1].Color3 = Color3.new(218, 133, 65)
end
end)
This is what the folder looks like:
I assumed when I put Head.Hair:GetChildren() it would retrieve the MeshPart in the folder, but it is not doing so.
The color property of BaseParts is named “Color”, not “Color3”. Also Color3s are constructed from numbers between 0 and 1.
The code should look like this:
local HairPart = char.Head.Hair:GetChildren()
for _, part in pairs(HairPart) do
if (part:IsA("BasePart")) then
part.Color = Color3.fromRGB(218, 133, 65)
end
end
Yes, there should only be one part in that folder at all times anyway since when you select a new hair it clears all existing children before adding a new one.
BasePart is an abstract class that encompasses all types of parts. That line is meant to ensure that the script won’t try to change the Color of any objects that don’t have a Color property.
However, you mentioned in a different reply that each hair will only contain one mesh part. If that’s the case then you don’t need to call GetChildren() at all, and you can just index the meshpart by name.
Your code worked, thank you so much. I replaced BasePart with MeshPart and it worked. Also, finding the MeshParts by name would be difficult since each hair would have a different name. Whenever you pick a new hair, it changes the name and mesh. i.e. Hair1, Hair2, Hair3. If that were the case I’d have to script in each color for each hair which would be too cumbersome.