Color giver script does not work?

So I have character customization in my game, and you can choose your skin color, but it does not work sometimes, When the player first joins, it works, but then after that it gives a different color, any reason why?

local bucnhOfColor = {
    [1] = "Pastel brown", --Default Shirt
    [2] = "Neon orange", -- Army green
	[3] = "Nougat", --Guess reckless
	[4] = "Reddish brown", --Hawaiian shirt
}

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)    
		local val = player:WaitForChild("stats"):WaitForChild("SkinColor").Value
            wait(0.5)
			if val >= #bucnhOfColor then
	character["UpperTorso"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["RightUpperArm"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["LeftUpperArm"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["RightUpperLeg"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["LeftUpperLeg"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["RightLowerArm"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["LeftLowerArm"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["RightLowerLeg"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["LeftLowerLeg"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["RightHand"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["LeftHand"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["RightFoot"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["LeftFoot"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
	character["Head"].BrickColor = BrickColor.new(bucnhOfColor[#bucnhOfColor])
			else
	character["UpperTorso"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["RightUpperArm"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["LeftUpperArm"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["RightUpperLeg"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["LeftUpperLeg"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["RightLowerArm"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["LeftLowerArm"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["RightLowerLeg"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["LeftLowerLeg"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["RightHand"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["LeftHand"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["RightFoot"].BrickColor = BrickColor.new(bucnhOfColor[val])
	character["LeftFoot"].BrickColor =  BrickColor.new(bucnhOfColor[val])
	character["Head"].BrickColor = BrickColor.new(bucnhOfColor[val])
	end
			end)
end)

You should be using a BodyColors object to do this rather than overwriting the colour of every single limb. Not only is it more convenient to use in the way that it automatically applies colouring to limbs, but it also prevents colour overwriting if you have this in (limb colours eventually revert to BodyColors colors).

Chances are that CharacterAppearanceLoaded aren’t firing. Have you done debugging to confirm this? There’s no defined problem in the thread to address, just “this script doesn’t work and I have no idea why”. Doesn’t help much in terms of explanation or finding a source.

1 Like

How would I do this? I never changed body colors.

Same way you’re currently changing it but in terms of BodyColor properties. Here’s a code sample (meaning you aren’t meant to copy and paste it, rather to salvage and learn from it or break it apart and fit it into your code in a way you want).

-- Your table of colours here. You don't need to make a dictionary if you're
-- using numerical keys, just make a standard array. {color1, color2, ...}
-- note: spelling mistake with bunch? it says bucnh of color
local bucnhOfColor = {}

-- Put the colourable properties of BodyColors in a table for convenience
local BODY_COLORS = {"HeadColor", "TorsoColor", "LeftArmColor", "LeftLegColor", "RightArmColor", "RightLegColor"}

-- Check for existing BodyColors or create new one just in case
local bodyColors = character:FindFirstChild("Body Colors") or Instance.new("BodyColors")

-- Iterate over the previous colours table and set colours
for _, bodyColorPart in ipairs(BODY_COLORS) do
    bodyColors[bodyColorPart] = BrickColor.new(bucnhOfColor[x])
end

-- Parent BodyColors if it has no parent (meaning it was created)
if not bodyColors.Parent then bodyColors.Parent = character end

I made this, but its an error at the “x”

local bucnhOfColor = {
    [1] = "Pastel brown", --Default Shirt
    [2] = "Neon orange", -- Army green
	[3] = "Nougat", --Guess reckless
	[4] = "Reddish brown", --Hawaiian shirt
}

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAppearanceLoaded:Connect(function(character)    
		local val = player:WaitForChild("stats"):WaitForChild("SkinColor").Value
            wait(0.5)
			if val >= #bucnhOfColor then
local BODY_COLORS = {"HeadColor", "TorsoColor", "LeftArmColor", "LeftLegColor", "RightArmColor", "RightLegColor"}

local bodyColors = character:FindFirstChild("Body Colors") or Instance.new("BodyColors")
for _, bodyColorPart in ipairs(BODY_COLORS) do
    bodyColors[bodyColorPart] = BrickColor.new(bucnhOfColor[x])
end

if not bodyColors.Parent then bodyColors.Parent = character end
			end
	end)
	end)

Naturally there’d be an error at x, because x isn’t a defined variable. That’s up to you to resolve by declaring x or replacing it with an appropriate value. A hint is that you do this exact thing in your old code. :slightly_smiling_face:

1 Like

Thank you, you saved my game! I appreiciate it. I marked your post as a solution.

2 Likes