How can I save a players hair color for character creator?

  1. What do you want to achieve?
    Saving hair color
  2. What is the issue?
    I have no idea how to get into the player to get their hair color and on top save it
  3. What solutions have you tried so far?
    Color3Values I have no idea how they work tho
local DataStore = game:GetService("DataStoreService"):GetDataStore("OnePieceYouTube_1")
local saveRemote = game:GetService("ReplicatedStorage"):WaitForChild("Save")

game.Players.PlayerAdded:Connect(function(player)
	
	local folder = Instance.new("Folder", player)
	folder.Name = "Data"
	
	local hasgame =  Instance.new("BoolValue", folder)
	hasgame.Name = "HasGame"
	
	local hair = Instance.new("StringValue", folder)
	hair.Name = "Hair"

	local outfit = Instance.new("StringValue", folder)
	outfit.Name = "Outfit"

	local accessory = Instance.new("StringValue", folder)
	accessory.Name = "Accessory"

	
	local success, errormsg = pcall(function()
		local getData = DataStore:GetAsync(player.UserId)
		for i, v in pairs(getData) do
			if i == 1 then
				hasgame.Value = getData[i]
			elseif i == 2 then
				hair.Value = getData[i]
			elseif i == 3 then
				outfit.Value = getData[i]
			elseif i == 4 then
				accessory.Value = getData[i]
			end
		end
	end)
	
	if success then
		print("Successfully loaded "..player.Name.."'s data!")
	elseif not success then
		hasgame.Value = false
		hair.Value = "Men Hair"
		outfit.Value = "White T-Shirt"
		accessory.Value = "Shades"
		error("Something went wrong while loading the data of "..player.Name..": "..errormsg)
	end
	
end)

saveRemote.OnServerEvent:Connect(function(player, val)
	DataStore:SetAsync(player.UserId, val)
	print("Successfully saved "..player.Name.."'s data!")
end)

Is my data saving script so far

1 Like

Actually I got it to get the HairColor Value but still can’t save and load it
“16:08:00.144 104: Cannot store Array in data store. Data stores can only accept valid UTF-8 characters. - Server - Data:68”

local char = workspace:WaitForChild(player.Name)
	if char then
		local hairt = char:WaitForChild("PlayerHair", 90000)
		if hairt then
			local hairc = hairt:WaitForChild("Handle")
			if hairc then 
				local haircolor = Instance.new("Color3Value", folder)
				haircolor.Value = hairc.Color
				haircolor.Name = "HairColor"

Is what im using to make a HairColor Value and

btn.MouseButton1Click:Connect(function()
	char.PlayerHair.Handle.Color = pls.ImageColor3
	script.Parent.Parent.Parent.Parent.Parent.Parent.Data.HairColor.Value = pls.ImageColor3
end)

Is what I use to set the value how do I save?

1 Like

You can’t save Color3 objects, you can save a dictionary with the R,G,B values of the Color3 though

local value = haircolor.Value
local color = {R = value.R, G = value.G, B = value.B}
1 Like

I also should’ve mentioned that I use an autosaving local script…

local Remote = game:GetService("ReplicatedStorage"):WaitForChild("Save")
local data = game.Players.LocalPlayer:WaitForChild("Data")
local enabled = false

while true do
	wait(10) -- TIME INTERVAL IN SECONDS
	local saveDataTable = {}
	table.insert(saveDataTable, 1, data.HasGame.Value)
	table.insert(saveDataTable, 2, data.Hair.Value)
	table.insert(saveDataTable, 3, data.Outfit.Value)
	table.insert(saveDataTable, 4, data.Accessory.Value)
	Remote:FireServer(saveDataTable)
	print("Autosaved "..game.Players.LocalPlayer.Name.."'s Data")
end 

And player’s spawn in bald

My attempt to load the character data

	local char = workspace:WaitForChild(Player.Name) -- the players character
	if char then -- if the character was found then
		for i, v in pairs(char:GetChildren())do -- we loop through the character
			for i, v2 in pairs(bodyParts) do -- we loop through the bodyparts list
				if v.Name == v2 then -- if the looped object's name in the character equals one of our body parts which need to be colored then
					v.BrickColor = BrickColor.new("Pastel brown") -- we change its color
				end
			end
		end
		
		local findHair = replicatedStorage:WaitForChild("Hair"):FindFirstChild(data.Hair.Value) -- we look for our saved hair in the hair folder
		if findHair then -- if it was found then
			local clonedHair = findHair:Clone() -- we copy it
			clonedHair.Name = "PlayerHair" -- change its name
			clonedHair.Parent = char -- put it into the character
			clonedHair.Color = data.HairColor.Value
		end