Saving a Color3Value in a Data Store

I’m trying to save Hair Color in a Color3Value in a Data Store. I was not familiar with Data Stores so I was going based on a template Data Store script I found. (Script works fine without the Color3Values) I would like to take this opportunity to learn more about Data Stores so please give as much explanation as to what I could have done better and how I can fix this issue.

The issue happens with the Player leaves the game (when the data is saved). The error it gives me is
Cannot store Dictionary in data store. Data stores can only accept valid UTF-8 characters. I don’t get this error when I have the Color3Value removed from the script.

Thanks for the help!

local DataStoreService = game:GetService("DataStoreService")

local playerData = DataStoreService:GetDataStore("PlayerData_BETATEST")



local function onPlayerJoin(player)  -- Runs when players join

	local PlayerData = Instance.new("Folder")  --Sets up leaderstats folder
	PlayerData.Name = "PlayerData"
	PlayerData.Parent = player



	local HasCreated = Instance.new("BoolValue") --Sets up value for leaderstats
	HasCreated.Name = "HasCreated"
	HasCreated.Parent = PlayerData
	
	local Outfit = Instance.new("IntValue") 
	Outfit.Name = "Outfit"
	Outfit.Parent = PlayerData

	local Hair = Instance.new("IntValue")
	Hair.Name = "Hair"
	Hair.Parent = PlayerData

	local HairColor= Instance.new("Color3Value") 
	HairColor.Name = "HairColor"
	HairColor.Parent = PlayerData





	local playerUserId = "Player_" .. player.UserId  --Gets player ID

	local data = playerData:GetAsync(playerUserId)  --Checks if player has stored data
	

	
	if data then
		HasCreated.Value = data['HasCreated']
		Outfit.Value = data['Outfit']
		Hair.Value = data['Hair']
		HairColor.Value = data['HairColor']
		
	else
		HasCreated.Value = false
		Outfit.Value = 1
		Hair.Value = 1
		HairColor.Value = Color3.fromHSV(0,0,1)

	end
	
end



local function create_table(player)
	local player_stats = {}
	for _, stat in pairs(player.PlayerData:GetChildren()) do
		player_stats[stat.Name] = stat.Value
	end
	return player_stats
end



local function onPlayerExit(player)  --Runs when players exit

	local player_stats = create_table(player)
	

	local playerUserId = "Player_" .. player.UserId

		playerData:SetAsync(playerUserId, player_stats) --Saves player data
end

--local success, err = pcall(function()

--	if not success then

--		warn('Could not save data!')
--	end
--end



game.Players.PlayerAdded:Connect(onPlayerJoin)

game.Players.PlayerRemoving:Connect(onPlayerExit)
1 Like

Can’t you just change it to a StringValue/NumberValue instead? It can pretty much work the same way I believe, it’s just that you’ll need to reference each of their individual colors:

local BlueColor = Instance.new("NumberValue")
BlueColor.Name = "BlueHairColor"
BlueColor.Parent = PlayerData
--Etc, etc do the same with the other 2 colors

local HairColor = Instance.new("Color3Value")
HairColor.Name = "HairColor"
HairColor.Parent = PlayerData

And when you want to obtain the colors, you can call GetAsync() as usual as you’ll be able to save Numbers in DataStores

BlueColor.Value = data["BlueHair"]
RedColor.Value = data["RedHair"]
GreenColor.Value = data["GreenHair"]

HairColor.Value = Color3.fromRGB(RedColor.Value, GreenColor.Value, BlueColor.Value)
2 Likes

I’ll try it out! But not entirely sure that would work for in the context I’m using it for (Apology’s should have put this in the main topic) The goal is to implement this color wheel that allows you to change hair colors

Yeah I’m aware, the issue here I believe is that you are currently unavailable to save Color3Values inside DataStores, which is why you have to think of alternate routes to go on saving the Data from a different standpoint

Thanks for the help, hope you have a wonderful Day/Night.

DataStore doesn’t save any Vector, Udim, Color, or any of these variable types from the little I’ve tested. You will have to serialize all of them before saving and when recovering you would have to do the reverse.