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.
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
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.”
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
local Gui = script.Parent
local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("BackgroundColorStore")
DataStore:SetAsync(Gui.BackgroundColor3.Value)
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 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.