Change color script?

So I have this scirpt that changes a players color depending on their value, but it doesn’t work, any help?

local bucnhOfColor = {
    [1] = "Pastel brown",
    [2] = "Neon orange",
	[3] = "Nougat",
	[4] = "Reddish brown",
}

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char)    
            wait(0.5)	
		local val = plr:WaitForChild("stats"):WaitForChild("SkinColor").Value
        if val >= 1 then
            wait(1)
			if val >= #bucnhOfColor then
		char["Body Colors"].HeadColor =  BrickColor.new(#bucnhOfColor[val])
		char["Body Colors"].LeftArmColor = BrickColor.new(#bucnhOfColor[val])
		char["Body Colors"].RightArmColor =  BrickColor.new(#bucnhOfColor[val])
		char["Body Colors"].LeftLegColor =  BrickColor.new(#bucnhOfColor[val])
		char["Body Colors"].RightLegColor = BrickColor.new(#bucnhOfColor[val])
		char["Body Colors"].TorsoColor =  BrickColor.new(#bucnhOfColor[val])
	end
end
	end)
	end)

Just remove the hashtag, it’s not needed. Secondly, you have made a dictionary; you need an array to use the # operator to find the number of elements on the table.

local bucnhOfColor = {
    "Pastel brown",
    "Neon orange",
	"Nougat",
	"Reddish brown",
}

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char)    
            wait(0.5)	
		local val = plr:WaitForChild("stats"):WaitForChild("SkinColor").Value
        if val >= 1 then
            wait(1)
			if val <= #bucnhOfColor then
		char["Body Colors"].HeadColor =  BrickColor.new(bucnhOfColor[val])
		char["Body Colors"].LeftArmColor = BrickColor.new(bucnhOfColor[val])
		char["Body Colors"].RightArmColor =  BrickColor.new(bucnhOfColor[val])
		char["Body Colors"].LeftLegColor =  BrickColor.new(bucnhOfColor[val])
		char["Body Colors"].RightLegColor = BrickColor.new(bucnhOfColor[val])
		char["Body Colors"].TorsoColor =  BrickColor.new(bucnhOfColor[val])
	    end
    end
  end)
end)

#scripting-support is not for getting code. Could you please explain in detail, what happens?

Is there any errors?

I would recommend using Color3 instead of brickcolor. And remove the hashtags

To clarify which hashtags people are talking about:

char["Body Colors"].HeadColor =  BrickColor.new(#bucnhOfColor[val])
char["Body Colors"].LeftArmColor = BrickColor.new(#bucnhOfColor[val])
char["Body Colors"].RightArmColor =  BrickColor.new(#bucnhOfColor[val])
char["Body Colors"].LeftLegColor =  BrickColor.new(#bucnhOfColor[val])
char["Body Colors"].RightLegColor = BrickColor.new(#bucnhOfColor[val])
char["Body Colors"].TorsoColor =  BrickColor.new(#bucnhOfColor[val])

To:

char["Body Colors"].HeadColor =  BrickColor.new(bucnhOfColor[val])
char["Body Colors"].LeftArmColor = BrickColor.new(bucnhOfColor[val])
char["Body Colors"].RightArmColor =  BrickColor.new(bucnhOfColor[val])
char["Body Colors"].LeftLegColor =  BrickColor.new(bucnhOfColor[val])
char["Body Colors"].RightLegColor = BrickColor.new(bucnhOfColor[val])
char["Body Colors"].TorsoColor =  BrickColor.new(bucnhOfColor[val])

The hashtag is used to get the length of a array or table, not convert a value to a number.

This doesn’t fix it though. How would I fix this?

Scripting support is not for us to throw a finished script at you. It’s meant to be a place to get help and feedback.

To help you, we need to see what the code outputs. This will tell us the issue with your code. You can find the output under view > output.

There is no output, it just doesn’t give me the color. No errors, nothing.

Add print statements 1-x to figure out what code does run. My guess is the if statements return false.

There’s two reasons as to why this didn’t work. One being, the elements in the table. The other being, char["Body Colors"].HeadColor & others accept a BrickColor value and not a Color3 value. To fix this, you could do BrickColor.new(StringColor).Color which returns a Color3 value of BrickColor StringColor.

-- ServerScript
local bunchOfColors = {
    BrickColor.new("Pastel brown"),
    BrickColor.new("Neon orange"),
	BrickColor.new("Nougat"),
	BrickColor.new("Reddish brown"),
}

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char)    	
		local val = plr:WaitForChild("stats"):WaitForChild("SkinColor").Value
		
		-- If val lower than 0, val = 1 (first value in table)
		if val <= 0 then
			val = 1
			
		-- If val greater than the number of vales in table, val = #table (last value in table)
		elseif val > #bunchOfColors then
			val = #bunchOfColors
		end

		char["Body Colors"].HeadColor =  bunchOfColors[val]
		char["Body Colors"].LeftArmColor = bunchOfColors[val]
		char["Body Colors"].RightArmColor =  bunchOfColors[val]
		char["Body Colors"].LeftLegColor =  bunchOfColors[val]
		char["Body Colors"].RightLegColor = bunchOfColors[val]
		char["Body Colors"].TorsoColor =  bunchOfColors[val]
	end)
end)

Hope this helps!

This is the culmination of @AbiZinho, @REALTimothy0812, @VegetationBush, and @sanjay2003’s feedback and work.

I take zero credit. I just ironed out some bugs in everyone’s code.

Working

local bunchOfColors = {
   BrickColor.new("Pastel brown"),
    BrickColor.new("Neon orange"),
	BrickColor.new("Nougat"),
	BrickColor.new("Reddish brown"),
	BrickColor.new("Really blue"),
}

game.Players.PlayerAdded:connect(function(plr)
	plr.CharacterAppearanceLoaded:Connect(function(char)
		
		local plrname = plr.Name
		local actualval = Instance.new("IntValue")
		actualval.Parent = workspace:WaitForChild(plrname) 	
		actualval.Value = 0
		local val = actualval.Value
		
		-- If val lower than 0, val = 1 (first value in table)
		if val <= 0 then
			val = 1
			
		-- If val greater than the number of vales in table, val = #table (last value in table)
		elseif val > #bunchOfColors then
			val = #bunchOfColors
		end

		char["Body Colors"].HeadColor =  bunchOfColors[val]
		char["Body Colors"].LeftArmColor = bunchOfColors[val]
		char["Body Colors"].RightArmColor =  bunchOfColors[val]
		char["Body Colors"].LeftLegColor =  bunchOfColors[val]
		char["Body Colors"].RightLegColor = bunchOfColors[val]
		char["Body Colors"].TorsoColor =  bunchOfColors[val]
	end)
end)
Disclaimer & Info
  • This will run only once. To modify it to run and always check, turn it into a loop.

  • This is susceptible to exploiters. It determines the color of the player based on a simple IntValue, which an exploiter could easily change as it’s stored on the client, not the server. You can fix this by creating the IntValue in ServerStorage.

  • The fatal flaw everyone’s code was the val variable was looking for a .Value, which didn’t exist.

Hope this helped!