Is it possible to save BackgroundColor3 values with DataStore?

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.

So I managed to create a script (with help from the replies):

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


local color3 = gui.BackgroundColor3
local DataStore = DataStoreService:GetDataStore("DataStore")
local function convertColor3ToTable(color3)
return {
	["R"] = color3.R,
	["G"] = color3.G,
	["B"] = color3.B,
}
end

local success, whoops = pcall(function()
	DataStore:GetAsync(convertColor3ToTable(color3) )
end)

if success then
	DataStore:UpdateAsync(convertColor3ToTable(color3) )
	print("Colors have been saved!")
else
	warn("An error occurred:", whoops)
end

But there is a problem:
image
There’s some problem with line 20.

Try this instead,

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


local color3 = gui.BackgroundColor3
local DataStore = DataStoreService:GetDataStore("DataStore")
local function convertColor3ToTable(color3)
return {
	["R"] = color3.R,
	["G"] = color3.G,
	["B"] = color3.B,
}
end

local data

local success, whoops = pcall(function()
	data = DataStore:GetAsync("BgColor-")
end)

if success then
    local savedColor = data
	DataStore:SetAsync("BgColor-", convertColor3ToTable(color3))
	print("Colors have been saved!")
else
	warn("An error occurred:", whoops)
end

It is still not working despite the output saying the success message.

It should be, it is, right now saving the data, it’s just it’s not loading any new data. I didn’t know if you wanted to load a colour so I just made a variable for savedColor if you wanted to change it.

1 Like

Ohhh. So the color is saved to the datastore but it just doesn’t load? Now how do I load it?

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


local color3 = gui.BackgroundColor3
local DataStore = DataStoreService:GetDataStore("DataStore")
local function convertColor3ToTable(color3_)
return {
	["R"] = color3_.R,
	["G"] = color3_.G,
	["B"] = color3_.B,
}
end

local data

local success, whoops = pcall(function()
	data = DataStore:GetAsync("BgColor-")
end)

if success then
    local savedColor = data
    gui.BackgroundColor3 = savedColor
	DataStore:SetAsync("BgColor-", convertColor3ToTable(color3))
	print("Colors have been saved!")
else
	warn("An error occurred:", whoops)
end