What do you want to achieve? I want to make a car customisation system where all the customisation is a table which can be saved and read to a datastore
What is the issue? I cant seem to make it work.
What solutions have you tried so far? I’ve tried searching devforum and using:
The main goal here is to have all the car data saved under 1 string value that can be stored, read and loaded into the game instead of making 15+ values just for 1 car.
Your gonna have to serialize the color values and then you can use HttpService:JsonEncode(table), to turn the table into a json string, then when you want to retrieve it you can use HttpService:JsonDecode(table)
Basically serialization is basically when you take one type of thing and turn it into something else so you can save it kinda thing so for example
So basically in JSON it only supports datatypes of string, number, booleans, null and arrays; so it cant support things like Color3s or CFrames or even Vector3s; youll have to turn them into tables first
function Color3ToTable(color3)
return {
R = color3.R,
G = color3.G,
B = color3.B,
}
end
then when you json decode it you would use this function to turn it back into a color3
function TableToColor3(table)
return Color3.fromRGB(table.R, table.G, table.B)
end
Im not sure how decoding works, what I want it to do is when u load ur car stats, it gets the string and reads the table then loads what settings are saved onto the car.
well since your not serializing and just have the value arbitrarily in the stats table, all you would need to do is just use JSONDecode and it would return the lua table you passed in before you JSONEncoded it
Most values have a __tostring metamethod in their userdata, so you can simply use the tostring function on any Color3, Vector3, or CFrame to create a string version of it.
Edit: I’d like to further add that you can use the string library to extract the number data from the saved string to create a new Color3 out of it. Here’s an example:
--- Function to convert Color3 to string
function color3ToString(color)
return string.format("Color3.new(%.2f, %.2f, %.2f)", color.R, color.G, color.B)
end
--- Function to convert string to Color3
function stringToColor3(str)
local values = {}
for val in str:gmatch("[^,]+") do
table.insert(values, tonumber(val))
end
if #values == 3 then
return Color3.new(values[1], values[2], values[3])
end
error("Invalid Color3 string format")
end
--- Example usage
local originalColor = Color3.new(0.5, 0.2, 0.8)
--- Convert Color3 to string
local colorString = color3ToString(originalColor)
print("Color3 as string:", colorString)
--- Convert string to Color3
local convertedColor = stringToColor3(colorString)
print("Converted Color3:", convertedColor)
color3s cant be serialized so you need to convert them into a different type before saving (unless you save the color as a HEX code which is a lot easier imo)