How To Save BrickColors or Color3 in a DataStore?

Currently having trouble with saving brickColors and Color3s in my datastore, and I really want to save them.

The reason for this, is I am planning on having character customization in my game tonight, such as color customization and such, but I keep getting the 104 error. Cannot Save Color3/BrickColor in DataStore.

Any help would be appreciated! :slight_smile:

8 Likes
function getSaveableColor3(color)
    return {r = color.r, g = color.g, b = color.b}
end

function loadColorFromDataStore(t)
    return Color3.new(t.r, t.g, t.b)
end
14 Likes

So what value types would these be inside of a DataStore? String?

Or would it be a table?

Table

3 Likes

Ah, right.

So would I then place that into a string?

This is currently how I have the datastore setup (pardon the bad color3 interpretation. Was trying something that didn’t work :joy:)

image
image

No.

datastore:SetAsync("SomeSavedColor", getSaveableColor3(Color3.new(...)))
local originalColor = loadColorFromDatastore(datastore:GetAsync("SomeSavedColor"))
2 Likes

if you do want strings for whatever reason, converting to hexadecimal is useful

local function colorToString(color)
	local r = math.floor(color.r*255+.5)
	local g = math.floor(color.g*255+.5)
	local b = math.floor(color.b*255+.5)
	return ("%02x%02x%02x"):format(r, g, b)
end

local function stringToColor(hex)
	local r, g, b = hex:match("(..)(..)(..)")
	r, g, b = tonumber(r, 16), tonumber(g, 16), tonumber(b, 16)
	return Color3.fromRGB(r, g, b)
end
7 Likes

I mainly need the Color3 to String so it can be saved inside of a datastore.

The two functions you posted should help with that. :slight_smile:

1 Like

In reference to @EchoReaper’s solution, I’m getting an error where value doesn’t exist allegedly.
I’ve posted the offending code segment below if anyone wants to help.


val.Parent=folder
val.Changed:Connect(function()if save then
local function getSaveableColor3(val)
    return {r = val.Value.r, g = val.Value.g, b = val.Value.b}
end
local function loadColorFromDataStore(t)
    return Color3.new(t.r, t.g, t.b)
end

Honestly not sure what to do if I can’t get one of these methods to work.

This is not enough information to go on. Please create a minimal repro file that has just the save/load color functionality and simple DataStore code (do not dump your whole game or script, as no one will want to read through the whole thing). As you create the minimal repro file, you’re more than likely find what the problem is, but if not, then it’s simple enough that someone can point out what you’re doing wrong.

1 Like

Here is a simplified version of the entire color storing thing.

Still the same error as described in my previous post.

local DS=game:GetService"DataStoreService":GetDataStore("PlayerCustomization","global")
game:GetService"Players".PlayerAdded:Connect(function(p)
local val = Color3.new(255,255,255)	
local function getSaveableColor3()
    return {r = val.r, g = val.g, b = val.b}
end
local function loadColorFromDataStore(t)
    return Color3.new(t.r, t.g, t.b)
end		
save=false;DS:SetAsync(p.UserId..getSaveableColor3())
delay(6,function()save=true;end)	
end)

Edit:
Changed it a bit and now I’m getting this error:

[ServerScriptService.DataStores.PlayerCustomizationDataStore:35: attempt to concatenate a table value])

1 Like

You’re using SetAsync wrong. SetAsync takes two arguments: key and value – you’re giving it one. Change:

DS:SetAsync(p.UserId..getSaveableColor3())

to

DS:SetAsync(p.UserId, getSaveableColor3(COLOR)

Note that you have to provide a Color3 to getSaveableColor3 – not empty arguments.

How do I get a color from a return?

I’ve never messed with return functions to be honest.

Edit:
It seems I’m one step closer to solving this. However, I’m getting a strange Color3 value that is black with numbers that appear to be 6365, 6365, 6365. What’s going on here?

local DS=game:GetService"DataStoreService":GetDataStore("PlayerCustomization","global")
game:GetService"Players".PlayerAdded:Connect(function(p)
local val = Color3.new(255,255,255)	


local function getSaveableColor3()
    return {r = val.r, g = val.g, b = val.b}
end
local function loadColorFromDataStore()
    return Color3.new(val.r, val.g, val.b)
end	

local data=loadColorFromDataStore()

local folder = Instance.new("Folder")
folder.Name = "PlayerCustomization"
folder.Parent = p
local val = Instance.new("Color3Value")
val.Parent = folder
val.Name = "PrimaryColor"
val.Value = data

save=false;DS:SetAsync(p.UserId, getSaveableColor3(val))
delay(6,function()save=true;end)	
end)

Color3.new takes values on a scale of [0,1]. If you want to use the scale of [0,255], you need to use Color3.fromRGB. Example: local val = Color3.fromRGB(255,255,255)

Oh yes.

I completely forgot about that since I’m trying to get the saving / loading to work.

Hmm…now the color is 0,0,0.

What am I doing wrong exactly?

local DS=game:GetService"DataStoreService":GetDataStore("PlayerCustomization","global")
game:GetService"Players".PlayerAdded:Connect(function(p)
local val = Color3.fromRGB(255,255,255)	


local function getSaveableColor3()
    return {r = val.r, g = val.g, b = val.b}
end

local function loadColorFromDataStore()
    return Color3.fromRGB(val.r, val.g, val.b)
end	

local data=loadColorFromDataStore()

local folder = Instance.new("Folder")
folder.Name = "PlayerCustomization"
folder.Parent = p
local val = Instance.new("Color3Value")
val.Parent = folder
val.Name = "PrimaryColor"
val.Value = Color3.fromRGB(loadColorFromDataStore())


val.Changed:Connect(function()
save=false;DS:SetAsync(p.UserId, getSaveableColor3(r, g, b))
delay(6,function()save=true;end)
end)	
end)
1 Like

If you look at the functions I originally posted, getSaveableColor3 takes one argument, “color”. You’re passing it three (r, g, and b), which aren’t defined as variables anywhere – they’re nil, and you removed the parameter in the function definition. When you construct a Color3 with nil arguments, it spits out black.

This is seeming less and less like you need help saving color3 values in DataStores, and more like you need help understanding all prerequisite knowledge necessary to implement this. I encourage you to watch these tutorials by AlvinBlox:



If you still have questions after watching those, feel free to ask here, but these should help out a lot.

3 Likes

Never messed with return functions.

Thanks for the tutorial videos. :slight_smile:

After a few minutes of watching the function video and browsing the wiki, things are looking good.

I’ll post my solution once I get everything working, but I am currently able to print the color from the value outside of the function.

Actually just going to finish up the rest of my game for now - hoping for official support of Color3 values eventually.

I might actually just do brickcolor codes since those can be stored as IntValues, I believe.

Thanks for the help though!

2 Likes