Having trouble with hair color data storage

So basically I’m wanting to randomize player’s hair color and save the color so that when the player joins again, they’d get the same hair color they got the first time they joined. But some really weird things happened when I tested the script below. Please help.

local hair_colors = {
	"Crimson";
	"Lime green";
	"Deep Sea";
	"Deep Orange";
	"Dark stone grey";
	"Really black";
	"Lily white";
	"Brown";
	"Lavender";
	"Bright Voilet";
} 
local new_color = hair_colors[math.random(1,#hair_colors)]

local DataStore = game:GetService("DataStoreService")
local PlayerInfoTable = {}
local rgkData = DataStore:GetDataStore("RogueGaKillTest")

game.Players.PlayerAdded:Connect(function(player)

	
	local data = Instance.new("Folder")
	data.Name = "Data"
	data.Parent = player
	local loadData
	
	local clan = Instance.new("IntValue")
	clan.Parent = data
	clan.Name = "Clan"
	local strength = Instance.new("IntValue")
	strength.Parent = data
	strength.Name = "Strength"
	local stamina = Instance.new("IntValue")
	stamina.Parent = data
	stamina.Name = "Stamina"
	local hunger = Instance.new("IntValue")
	hunger.Parent = data
	hunger.Name = "Hunger"
	local speed = Instance.new("IntValue")
	speed.Parent = data
	speed.Name = "Speed"
	local defense = Instance.new("IntValue")
	defense.Parent = data
	defense.Name = "Defense"
	local special = Instance.new("IntValue")
	special.Parent = data
	special.Name = "Special"
	local hairColor = Instance.new("StringValue")
	hairColor.Parent = data
	hairColor.Name = "HairColor"
	local success, err = pcall(function()
		loadData = rgkData:GetAsync(player.UserId)
	end)


	if success then
		if type(loadData) == "table" then
			clan.Value = loadData[1] or 1
			strength.Value = loadData[2] or 0
			stamina.Value = loadData[3] or 0
			speed.Value = loadData[4] or 0
			defense.Value = loadData[5] or 0
			special.Value = loadData[6] or 0
			hunger.Value = loadData[7] or 100
			hairColor.Value = loadData[8] or new_color
		else
			clan.Value = 1	
			strength.Value = 0
			stamina.Value = 0
			speed.Value = 0
			defense.Value = 0
			special.Value = 0
			hunger.Value = 100
			hairColor.Value = new_color
		end
	end
--MAIN HAIR COLOR LOADER
	print(hairColor.Value)
	print(new_color)
	player.CharacterAppearanceLoaded:Connect(function(char)
		for _,v in pairs(char:GetChildren()) do
			if v:IsA("Accessory") then
				if v.Handle:FindFirstChild("HairAttachment") ~= nil then
					local mesh = v.Handle:FindFirstChild("Mesh") or v.Handle:FindFirstChild("SpecialMesh")
					if mesh then
						mesh.TextureId = ""
						print("Removed Texture")
					end
					v.Handle.BrickColor = BrickColor.new(hairColor.Value)
					print("Applied new hair color")
				end			
			end
		end
	end)

	PlayerInfoTable[player.UserId] = {
		clan.Value, 
		strength.Value, 
		stamina.Value, 
		speed.Value, 
		defense.Value, 
		special.Value, 
		hunger.Value,
		hairColor.Value
	}
	
	clan.Changed:Connect(function(value)
		PlayerInfoTable[player.UserId][1] = value 
	end)
	strength.Changed:Connect(function(value)
		PlayerInfoTable[player.UserId][2] = value 
	end)
	stamina.Changed:Connect(function(value)
		PlayerInfoTable[player.UserId][3] = value 
	end)
	speed.Changed:Connect(function(value)
		PlayerInfoTable[player.UserId][4] = value 
	end)
	defense.Changed:Connect(function(value)
		PlayerInfoTable[player.UserId][5] = value 
	end)
	special.Changed:Connect(function(value)
		PlayerInfoTable[player.UserId][6] = value 
	end)
	
	hunger.Changed:Connect(function(value)
		PlayerInfoTable[player.UserId][7] = value 
	end)
	
	hairColor.Changed:Connect(function(value)
		PlayerInfoTable[player.UserId][8] = value 
	end)
end)	

game.Players.PlayerRemoving:Connect(function(player)
	local data = PlayerInfoTable[player.UserId]

	if data then
		rgkData:UpdateAsync(player.UserId, function(oldData) return data end)
	end
end)

Edit: The problem is how the hair loader does not load the color. I still can’t find out why…

Can you explain more about this “weird issue” you are getting? I looked over the script and I dont see anything that would cause errors.

Edit: Actually I just noticed something!

In your .Changed connections you are using value which is not the value but the property.
so you will need to set the table data to hairColor.Value instead of just value but for the other stats as well.

I edited something in the post, thanks for the reply anyways!

There is a chance the hair is not a Part with a Mesh inside, but rather a MeshPart itself. So you would remove MeshPart.TextureID and then change the BrickColor.

if handle.ClassName == "MeshPart" then

Didn’t quite get that, from what I know every hair on Roblox have 4 basic parts, those are the Accessory object, the Handle, the HairAttachment and the SpecialMesh/Mesh.

Some accessories are different, as seen here:
image

Oh alright, really useful information; thanks a lot! But the problem is the structure of my hair is exactly the same as how my script works. (sorry for my bad english :skull:) image

That’s why you have to check for both. :slight_smile: