How to turn a table into a string

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:

table.concat()

but this dosen’t work

This is the table im using as a test:

local CarStats = {
	CarName = "FORD FOCUS RS",
	CarPrimaryColor = Color3.fromRGB(255,90,255),
	CarSecondaryColor = Color3.fromRGB(188, 66, 188),	
	CarEngineLevel = 5,
	LicencePlate = "LN58 LPV",	
}

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.

maybe try table.unpack and then have a string value store the following?

like this:

local example = tostring(table.unpack(CarStats))

i dont know if that will work, i really am just a starter dev but hey, who knows!

1 Like

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)

https://create.roblox.com/docs/reference/engine/classes/HttpService#JSONEncode
https://create.roblox.com/docs/reference/engine/classes/HttpService#JSONDecode

1 Like

How can i serialize colour values?

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
1 Like

Ok so this is what the script has changed into, It sets the string value into a table:

local http = game:GetService("HttpService")


local CarStats = {
	CarName = "MYFOCUS",
	CarPrimaryColorR = 255,
	CarPrimaryColorG = 90,
	CarPrimaryColorB = 255,
	CarSecondaryColorR = 188,
	CarSecondaryColorG = 66,
	CarSecondaryColorB = 188,
	CarEngineLevel = 5,
	LicencePlate = "LN58 LPV",	
}

task.wait(1)
local EncodedStats = http:JSONEncode(CarStats) 
game.Workspace.FordStats.Value = EncodedStats

So if im Decoding it, how do i make it load 1 bit at a time?
So example
Load Primary colors, then secondary colors, then Engine level e.t.c

Why do you need to load them in order?

1 Like

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

Maybe show more of the script?

One second im testing something i saw on documentation

1 Like

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)
2 Likes

Ok so ive tried to test a decode and i cant Decode the String table.

EDIT:
It looks like i have a result
image

@astraIboy

image

How would I use this if message == “success” then part? and how does it work?

I might have a way of making it work. It seems to be working at the moment.

1 Like

The message property is only relevant for HTTP Web Requests; in your case, it’s not needed

1 Like

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)

1 Like

Ive got a solution to the color3s, but thanks

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.