Help me save a color3 value in DataStore

I’m trying to make a “theme” Option in my game. That’s why I need the color3 value to save. I know that you can’t directly save a color3 value in datastore so that’s why I need help

My script:
– I almost no idea how to work with data stores so don’t mind if the script is bad… I copied it from my cash system so yea😭

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



game.Players.PlayerAdded:Connect(function(Player)



    local ColorFolder = Instance.new("Folder")
	ColorFolder.Name = "ColorFolder"
	ColorFolder.Parent = Player
	
	local UpdateEvent = Instance.new("RemoteEvent")
	UpdateEvent.Name = "UpdateEvent"
	UpdateEvent.Parent = ColorFolder
	
	
	local Color = Instance.new("Color3Value")
	Color.Name = "ColorValue"
	Color.Value = Color3.fromRGB(252, 60, 68) 
	Color.Parent = ColorFolder
	
	Color.Changed:Connect(function()
		UpdateEvent:FireClient(Player) -- Temporary way to test other things out of topic
	end)
	
	
	
	local Data = DataStore:GetAsync(Player.UserId)
	
	if type(Data) ~= "table" then
		Data = nil
	end
	
	if Data then
		Color.Value = Data.Color
	end
	
end)




game.Players.PlayerRemoving:Connect(function(Player)
	

	
	DataStore:SetAsync(Player.UserId, {
		Color = Player.ColorFolder.ColorValue.Value,
	})
	
end)

long ago i used this method:
if i want to save a color3, i will covert it to a string that my script can read. for an example rgb 13, 56, 146 turns to "13,56,146".
when i want to retrieve the rgb code, i just do:

local code = "13,56,146" 
local split = code:split(",")
local rgb = Color3.fromRGB(tonumber(split[1]), tonumber(split[2]), tonumber(split[3]))

(the answer below is a better solution.)

3 Likes

Tbh, just use HexCode, doesnt take as much data as saving a normal RGB Value the way it is, and if you want to convert that into a Color3 Again:

Color3.new(1, 0, 0):ToHex() -- ff0000 (Hex Code)

Color3.fromHex("ff0000") -- 255, 0, 0 / 1, 0, 0 (to RGB)
5 Likes

OMG thxxx I’ve never even thought of saving it as hex and that would make most sense as it’s a string value​:person_facepalming::person_facepalming: And by the way could you help me with this one small error I get? I want a script to change a color3 value but it just does nothing at all

I’m gonna mention that everything in the phone thingy changes color but the value itself doesn’t which is the problem because I need it to change so other parts of the game know what color to change to.
Mention - It Doesn’t throw any errors
Both ColorValue and ColorVal are a color3 value so idk if my knowledge abt this bad or idk so please help if you can.

-- I'm not gonna include all the locals cuz it's a big script blah blah --
local ColorFolder = Player:WaitForChild("ColorFolder")
local ColorValue = ColorFolder:WaitForChild("ColorValue").value
local UpdateEvent = ColorFolder:WaitForChild("UpdateEvent")

for i,v in pairs(ThemePage:GetDescendants()) do
	if v:IsA("TextButton") then
		v.MouseButton1Click:Connect(function()
			wait() -- Giving it time to wait cuz sometimes people like to spam click stuff
			ColorValue = v.ColorVal.value -- THIS PART DOESN'T WORK  |  Color3 value stored inside a button
			UpdateEvent:FireServer(Player)
		end)
	end
end

Pictures to visualize it.


Picture2

1 Like