How to save Color3 value in Datastore

hey i have a question

how do I save a color3 value to a DataStore?? this line errors:

local Players = game:GetService("Players") -- player.Parent
local Datastore = game:GetService("DataStoreService")

Datastore = Datastore:GetDataStore("colors")

-- other code

Players.PlayerRemoving:Connect(function(pleyer)
	local ok, eror = pcall(function()
		Datastore:SetAsync(pleyer.Name,pleyer.info.color.Value)
	end)
	if not ok then
		print(eror) -- line that errors
	end
end)
3 Likes

First of all, you have a few bad habits in your code.

You should instead make a new variable with the Datastore’s name instead of setting the previous variable that had the service.

Here, you should use Player’s (or pleyer’s, as you for some reason use it) UserId, since names can change and I don’t think you want players to lose the “color” data you store.


Now, let’s see how you save a Color3 value (as RGB):
When encoding, it is pretty straight forward:

local Color = Color3.fromRGB(0,255,0)
Color = ("%s,%s,%s"):format(tostring(Color.R),tostring(Color.G),tostring(Color.B))
local Success,Error = pcall(function()
	Datastore:SetAsync(tostring("Color_"..Player.UserId),Color)
end)
if not Success then warn(Error) end

And same for decoding:

local Color = nil
local Success,Error = pcall(function()
	Color = Datastore:GetAsync("Color_"..tostring(Player.UserId))
end)
if not Color then Color = Color3.fromRGB(255,255,255) end
-- See, when the encoder saves the data, it's not saved as .fromRGB format. You need .new
Color = Color:split(",")
Color = Color3.new(tonumber(Color[1]),tonumber(Color[2]),tonumber(Color[3]))
Player.info.color.Value = Color

And there you go.


For the love of code, write your variables normally.

1 Like

okey thanks bro appreciate it! goodluck on your games and script!

I don’t see any error, but I think the pleyer instead of player could cause this.

1 Like

no it would normally error if i dont add the pcall look:
image

Try using sort of a string acting as a Vector3 to coverup the color (colorX, colorY, colorZ) that will act as a color which then you can use it to convert it to a Color3 value

ok so i store the Color3 as a Vector3 and then the R becomes X and G is Y and B is z? but how do i store it

Personally what I do is:

local color = Color3.fromRGB(116,8,8)

local function Serialize(color)
   local r,g,b = color.R,color.G,color.B
   return r..","..g..","..b
end

local function Deserialize(colorstring)
   local r,g,b = colorstring:split(",")
   return Color3.new(r,g,b)
end

local stringcolor = Serialize(color)
Deserialize(stringcolor)

I suppose?

The XYZ were just an example. I can’t really describe it, but here is an example:

local value = Vector3.new(3,3,3) -- format 1
local value2 = "R,G,B" -- not sure if usable
local value3 = math.abs(r,g,b)

That’s what I thought of.

i think it cannot create a color3 from string? is that new
return r..","..g..","..b

@vxcaie and what value do i store in the datastore

Sure it can!

local color = Color3.fromRGB(116,8,8)

local function Serialize(color)
	local r,g,b = color.R,color.G,color.B
	return r..","..g..","..b
end

local function Deserialize(colorstring)
	local r,g,b = colorstring:split(",")[1],colorstring:split(",")[2],colorstring:split(",")[3]
	return Color3.new(tonumber(r),tonumber(g),tonumber(b))
end

local stringcolor = Serialize(color)
local c = Deserialize(stringcolor)
print(color,stringcolor,c)

Fixed code, yes the color is equal to c i.e deserialized serialized color.

You can store a NumberValue or IntValue under what i’ve stated.

(not sure if this is true)

i gotta make some function to store instance now?
image
@NoxhazeI print works but how do i store it and get it
also what is “cerealized”

What value type do you use to store the color values?

in datastore or in pleyer.info ? i use Color3Value

I have just googled what is UTF-8 apparently, it’s like Unicode. It’s encoding stuff allocated to letters and symbols.

i dont know it looks like some sort of swear said 8 times

why is roblox swearing at me

Thus, this means that you have put a certain character that ROBLOX may not accept, for example, ROBLOX does not validate Punjabi or Hindi.

1 Like

Just store it like a normal string, its not that hard… Serialize means the process of converting object state into a format that can be transmitted or stored

1 Like

ok ill see how would i unstore it when getasync is called, thanks! good luck on your projects and clothing and script!