Saving Color3 to DataStore2

I am trying to save a Color3 to DataStore2, which then will be applied to HumanoidDescription colors, which will be applied to the character.

Everything else(Face, shirts, Hair and etc.) works fine, but i cant make the colors work

Here are the relevant parts of the code

local DataStore2 = require(ServerScriptService.DataStore2)
local defCust = {Gender = "Male", Face = "86487700", Hair = "0", Color = "248, 248, 248", Shirt = "http://www.roblox.com/asset/?id=5583364865", Pants = "http://www.roblox.com/asset/?id=1838299111"}
local defQuest = {Quest1 = 0, Quest2 = 0, Quest3 = 0, Quest4 = 0, Quest5 = 0, Quest6 = 0, Quest7 = 0}


DataStore2.Combine(
"DATA", "xp", "nextxp", "lvl", "souls", "gold",
	"strength", "toughness", "agility", "wisdom",
	"charcustom", "quest"	
)

And

local humDescrip = nil
	
	if charcustTable["Gender"] == "Male" then
		humDescrip = game.ReplicatedStorage.Customization.Gender:WaitForChild("Male"):Clone()
	else if charcustTable["Gender"] == "Female" then
			humDescrip = game.ReplicatedStorage.Customization.Gender:WaitForChild("Female"):Clone()
		end
	end
	humDescrip.Name = "ThisDescrip"
	humDescrip.Face = charcustTable["Face"]
	humDescrip.HairAccessory = charcustTable["Hair"]
	local col = charcustTable["Color"]
	print(col)
	humDescrip.HeadColor = col
	humDescrip.LeftArmColor = col
	humDescrip.LeftLegColor = col
	humDescrip.RightArmColor = col
	humDescrip.RightLegColor = col
	humDescrip.TorsoColor = col
	humDescrip.Parent = custfolder

the problem is local col = charcustTable[“Color”]. It cant turn string into Color3.new or work this way. I have also tried with tonumber(), once again, to no avail

how can i save a color3 to datastore2, or optionally, convert string into color3

1 Like

To save colors, your best bet is to save it as a string. When you want to retrieve the data, you need to convert the string back to a Color3 object.
Example:

local function convertColorToString(color)
    return tostring(color):gsub(" ", ""); -- using tostring() on Color3 returns a string with its rgb values, I'm using gsub() to eliminate the spaces in the string.
end
local function convertStringToColor3(str)
    return Color3.new(table.unpack(str:split(","))); -- the values in the string are seperated by a ",", remember?
end

so in your second function i just need to change the parameter str with the string value?

Weird thing happened
i did this

local col = Color3.new(table.unpack(charcustTable["Color"]:split(",")))	
	print(col)
	humDescrip.HeadColor = col
	humDescrip.LeftArmColor = col
	humDescrip.LeftLegColor = col
	humDescrip.RightArmColor = col
	humDescrip.RightLegColor = col
	humDescrip.TorsoColor = col

And this is the result i got. Pay attention to the color values in the hierarchy

Change this to

Color3.fromRGB
2 Likes

Thank you both, it worked, the colors were 248, 248, 248

However in the output i got this

0.972549, 0.972549, 0.972549

im just curious why? Can you please explain?

Weird, I tested it out and it worked perfectly fine.

local datastoreService = game:GetService("DataStoreService");
local colorStore = datastoreService:GetDataStore("color_1");

local function convertColorToString(color)
	return tostring(color):gsub(" ", ""); -- using tostring() on Color3 returns a string with its rgb values, I'm using gsub() to eliminate the spaces in the string.
end

local function convertStringToColor3(str)
	return Color3.new(table.unpack(str:split(","))); -- the values in the string are seperated by a ",", remember?
end

local rColor = colorStore:GetAsync("color");
if rColor then
	rColor = convertStringToColor3(rColor);
else
	rColor = Color3.new(math.random(), math.random(), math.random());
end

local colorStr = convertColorToString(rColor);
print(colorStr);
colorStore:SetAsync("color", colorStr);

script.Parent.Color = rColor;

Maybe what @anthropomorphic_dev said can be your solution. If it is, then you’ll have to change up the convertColor3ToString() code a little bit too.

1 Like
local col = Color3.fromRGB(table.unpack(charcustTable["Color"]:split(",")))	

This has changed the colors to my desired values, it worked on humanoid description too, but when i printed it i got

0.972549, 0.972549, 0.972549

Why?

Color3.new() has R, G and B values between 0, 1. However, Color3.fromRGB() accepts values from 0 to 255.

To convert a Color3 value from the 0-1 scale to 0-255 scale, just multiply the value with 255 and round it down to get rid of the decimals.

To alter the code I provided so that it saves RGB values, change the convertColor3ToString() function to:

local function convertColorToString(color)
	local r = math.floor(color.R * 255);
	local g = math.floor(color.G * 255);
	local b = math.floor(color.B * 255);
	
	return ("%i,%i,%i"):format(r, g, b);
end

and in the convertStringToColor3() code you just have to change .new() to .fromRGB() like so:

local function convertStringToColor3(str)
	return Color3.fromRGB(table.unpack(str:split(",")));
end
2 Likes

Printing Color3 values gets their value as a percentage from 0-255
So if your color is 248, 248, 248 then when you print the color3 it will show up as
0.972549, 0.972549, 0.972549

Not sure if you’re aware but split’s separator/delimiter argument is automatically a comma.

1 Like

I didn’t know that, definitely going down in my brain! :+1:

Thank you all, the important thing is it worked <3

i thick he can Format it to Hex