How do you save a the BackgroundColor3 value of a gui in a Datastore?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    I want to make a script that when a localscript changes the color of a gui it saves in a datastore
  2. What is the issue? Include screenshots / videos if possible!

The issue is that I have no clue on how to serialize and save it in the datastore as I look for tutorials on serialization like this one:How to save parts, and the idea of Serialization which confusing even though I had a basic understanding of tables and functions.This is the problem I have can someone simplify it for me so I can understand

  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I have tried using values that when the color changes it tries to save in a Datastore but that did not work.
DataStore:SetAsync("color", gui.BackgroundColor3)

This code does not work because you can’t save color 3 values that way as it would give an error this is why I need help with serialization to condense it into a form which the datastore can save in

DataStore:SetAsync("color_r", gui.BackgroundColor3.R)
DataStore:SetAsync("color_g", gui.BackgroundColor3.G)
DataStore:SetAsync("color_b", gui.BackgroundColor3.B)

Can you provide an in-depth explanation of what you actually explain doing and applying it in a datastore

In-depth? I’m just saving the individual R, G, & B values of the Color3.

Color3 contains the RGB components as properties. You can serialise the Color3 by saving a table of the RGB components; when needing to load the data and use the Color3, you can create a Color3 off of the saved components.

Color3 can’t be saved to a dictionary itself because it’s not a serialisable datatype so you have to serialise it by hand. That’s what R0blox is doing, just don’t ever use three individual DataStore keys to do this. You can save it to one single table that’s part of the player’s overall data.

local colour = Color3.new(1, 0.5, 0.5) -- Oops, Random wasn't a constructor, lol
local savedColour = {R = colour.R, G = colour.G, B = colour.B}
local loadedColour = Color3.new(savedColour.R, savedColour.G, savedColour.B)

There’s a way to do this with table unpacking but I dislike that method since it’s not entirely clear where your values are slotted to.

3 Likes

Alright it actually starting to sound more clear now so with the backgroundcolor 3 so I just get async the savedColors and I set async the loaded colors right?

Yeah, just don’t do them individually though. Ideally when saving data you should have one big table full of all the player’s data and you can make this serialised Color3 a part of that data. If you use SetAsync on each component individually, you will trip your budget quickly.

You would GetAsync the data table and pull the saved color’s R, G, & B values from it. From there, you would reconstruct the Color.

Alright I will let you know if I have any problems with it but should I put in a leader stats under a folder