Is it possible to save BackgroundColor3 values with DataStore?

So I am making a frame with a datasaving mechanism. I am trying to save its Background Color. Here’s my script:

local gui = script.Parent
local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStore")

DataStore:SetAsync(gui.BackgroundColor3)
-- and... I don't know what else to add. Of course I did something wrong.

You need to do what’s called serialization. Basically, convert an unsavable valuable to a saveable value.

function convertColor3ToTable(color3)
    return {
        ["R"] = color3.R,
        ["G"] = color3.G,
        ["B"] = color3.B,
    }
end
5 Likes

It’s not possible to save a Color3 constructor inside a DataStore, but what you can do is separate all the R, G, and B numbers of the Color3 instead inside of a table

Oh yeah, if you’re doing this on a LocalScript the DataStore functions won’t be able to properly work correctly, make sure to do it on a regular Script

Try this:

local gui = script.Parent
local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStore")
local Colors = {
    gui.BackgroundColor3.R,
    gui.BackgroundColor3.G,
    gui.BackgroundColor3.B,
}

local success, whoops = pcall(function()
    DataStore:SetAsync("Gui Colors", gui.BackgroundColor3)
end)

if success then
    print("Colors have been saved!")
else
    warn("An error occured:", whoops)
end

Also SetAsync requires 2 parameters, the specific key string you’re wanting to set, and the value of that key

3 Likes
local image = script.Parent
local imageBC3 = image.BackgroundColor3
local imageBC3toRGB = {imageBC3.R, imageBC3.G, imageBC3.B}

DataStore:SetAsync("GuiBC3"..player.UserId, imageBC3toRGB)

Just an example script but this is how you’d serialise a Color3 value into a format compatible with DataStores.

3 Likes

The script makes an error. The script has a table that converted it into a savable value (As said by the first comment, @Pokemoncraft5290 ) but it’s not working?
This is what it said. :
“An error occurred: 104: Cannot store Color3 in data store. Data stores can only accept valid UTF-8 characters.”

1 Like

Replace the second parameter with “Colors”.

1 Like

Okay now the output says the success message yet it didn’t even save even if it said success.

1 Like

Whoops, forgot to save the actual table

That’s cause it’s saved within the Datastores of that specific key, if you want to retrieve it you’ll have to call GetAsync() with the first parameter being the “Gui Colors” as the specific key

You can reference a Data variable that we’ll later use to get the data if there is any, using another pcall function

-- Somewhere far far away in another random script....
local DataStoreService = game:GetService("DataStoreService")

local DataStore = DataStoreService:GetDataStore("DataStore")

local Data
local success, whoops = pcall(function()
    Data = DataStore:GetAsync("Gui Colors")
end)

print(Data) --So that we know that it's loaded or nil

if success and Data then
    print("Colors have been loaded!")
else
    warn("An error occured:", whoops)
end
1 Like
local Gui = script.Parent
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("BackgroundColorStore")

DataStore:SetAsync(Gui.BackgroundColor3.Value)

If it doesn’t work, let me know!

1 Like

Okay now I’m getting confused. Shouldn’t there be a SetAsync somewhere in the script so it saves the data? (sorry if it sounded rude)

Yes, I assume @JackscarIitt just used it as an example.

1 Like

Maybe try testing it outside of studio?

Tried it and still doesn’t work.

Is the script a localscript? Only scripts can access datastores.

The code you have presented us is not working because you have to be in the studio for more than 10-15 seconds for data to save. However, if you were to be in-game, this would not be an issue. Moreover, the next problem is the logic behind attempting to save a data structure that is not supported. You can only keep strings, numbers, and bools. Conversely, you are trying to save a Color3 to save a Color3; please refer to the other comments stating how to fix the issue.

You can also serialize the Color3 as a hex string.


1 Like

It’s in a normal script. It is under Serverscriptservice too.

All I can think of is you setting the backgroundcolor3 from the local player, other than that you should rely on the other answers provided.

Those are 2 resources you should check out, they are what you are looking for!

1 Like

You could save it in a table, DataStores can support tables, with data inside of it as long as there are no instance data inside of the table. Your is a color3 value so if you save a table to it with BackgroundColor3 value inside of it it should work.