How do I save a color3 value?

You can save the individual red, green, and blue values.

1 Like

What do you mean by “save”? Do you want to save it to a datastore?

1 Like

You can save this

local toSave = {color.R, color.G, color.B}

And then convert it back to a Color3

local color = Color3.new(toSave[1], toSave[2], toSave[3])
2 Likes

Yes but it says unable to save blah blah blah something utc 8 characters I don’t remember what it said

So a string? then converting it to color3? I’ll try

You can save the RGB values separately in an array.

local color = Color3.new(1, 1, 1)

local array = {color.R, color.G, color.B}

pcall(function()
    datastore:SetAsync(key, array) -- assuming you  have a key and datastore var stored
end)

To to retrieve that data:

pcall(function()
    local data = datastore:GetAsync(key) -- assuming you  have a key and datastore var stored
end)

local color = Color.new(data[1], data[2], data[3]

The same concepts applies to vector3s, cframes, etc…

Your code will have much more to it, but this is the basic concept.

2 Likes

It just prints: ServerScriptService.Script:17: attempt to index nil with number

Can you give your whole script?

You need to use HttpService:JSONEncode(value) when saving it to a datastore, and HttpService:JSONDecode(value) when using GetAsync, as color values cannot be saved to datastores

Can’t you save each number(each rgb value) separately and then when loaded convert it to Color3?

A simple datastore should work iirc:

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("ColorSaveStore")

function Save(Key, Color) -- Key is what the data gets saved by.
  local Fired, Result = pcall(function() -- Always use pcall's when saving data.
    DataStore:UpdateAsync(Key, function(OldValue)
      return Color
    end)
  end)
  if Fired then
    print("Saved Successfully!")
  else
    warn("Save Failed, Message: " .. Result)
  end
end

local Color = Color3.new(1, 1, 1) -- Change this to your color.
-- To Save, Just Run --
Save(Player.UserId .. "_Save", Color)

Note: The Variable Player is just the player who is getting their color saved.

Take a look at this for more info:

What’s your entire script. I dont know what you have.

Example:

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

Source: How To Save BrickColors or Color3 in a DataStore?

I suggest searching on your own before posting a thread as most questions on the devforum is already answered.

You can’t save userdatas such as Color3s, CFrames, Vector3s etc. The parser which saves the DataStores will automatically make them null

Ohhhh, well then can’t you just turn the color into a string and then when you need the color split it apart?

local Color = tostring(Color3.new(1, 1, 1))
Save(Key, Color)

and then to get it

local Data = DataStore:GetAsync(Key)
local Split = string.split(Data, ",")
local R, G, B = tonumber(Split[1]), tonumber(Split[2]), tonumber(Split[3])

You can do that but it’s mostly preferable to just save the values individually to a single table and save that, then load the data store and create a new color3 with the values in the table. Basically what you did but different

1 Like

Ohh ok, makes sense, so just save the whole color in an array, and then split it when you need it?

1 Like

That should work too, but you would need separate datastores, each for R, G & B.
Or just save them to a table then use JSONEncode

Thank you! This helped me with one of my projects.

1 Like

Hey!

A better way to save colors in a data store would be saving the color as a string with the colors hex value. If you’re looking for something probably more cleaner and optimized.

1 Like