How to save properties of a certain item?

So, I have a DataStore that efficiently saves data from a given folder. For example, if the folder was named ‘Cars’ and had BoolValues with their .Name property being the CarName, the data saved would be this:

["Cars"] =  ▼  {
                       ["Camaro"] = false,
                       ["Lamborghini"] = false,
                       ["Audi"] = false,
                       ["Tesla"] = false,
                    },
-- The table being saved currently. Works perfectly.

But I want to have properties of each car stored within the variable. In the end, I get this as the data about to be saved:

["Cars"] =  ▼  {
                       ["Camaro"] = ▼  {
                                 ["WindowColour"] = Color3.fromRGB(0, 0, 0)
                                 ["SuspensionHeight"] = 5     
                       },
                       ["Lamborghini"] = ▼  {
                                 ["WindowColour"] = Color3.fromRGB(0, 0, 0)
                                 ["SuspensionHeight"] = 10     
                       },
                       ["Audi"] = ▼  {
                                 ["WindowColour"] = Color3.fromRGB(0, 0, 0)
                                 ["SuspensionHeight"] = 8     
                       },
                       ["Tesla"] = ▼  {
                                 ["WindowColour"] = Color3.fromRGB(0, 0, 0)
                                 ["SuspensionHeight"] = 7     
                       },
                    },

Unfortunately, when I try to save this, I get the error “Cannot store Dictionary in data store . Data stores can only accept valid UTF-8 characters.

Is there a better way to save data like this?

So like the error said you can’t save it as a dictionary, convert it to a string.

Use:
game:GetSevice("HTTPService"):JSONEncode(dict)

2 Likes

Serialize your color3 values into strings or a table and deserialize when requesting for it’s data

function serialize(color)
   return string.format("%s_%s_%s", color.R, color.G, color.B)
end

function deserialize(colorStr)
   local tbl = string.split(colorStr, "_")
   return Color3.fromRGB(tonumber(tbl[1]), tonumber(tbl[2]), tonumber(tbl[3]))
end
2 Likes

The %s format specifier is for strings, use %f for floats.

Just tested this out and it works like a charm! Thank you so much for the idea!

@TenBlocke I will surely use your technique to convert colour values to string as JSONEncode doesn’t seem to do that for me. Thank you all so much for the help!

1 Like