How would I go about saving color3 data?

I have this script:

local ds = game:GetService("DataStoreService"):GetDataStore("carsave")

game.Players.PlayerAdded:Connect(function(player)
	local data = Instance.new("Folder", player)
	data.Name = "cars"
	
	
	local corvan = Instance.new("IntValue", data)
	corvan.Name = "oldvan"
	local corvancolor = Instance.new("Color3Value", data)
	corvancolor.Name = "oldvancolor"
	
	local vans = Instance.new("IntValue", data)
	vans.Name = "van"
	local vancolor = Instance.new("Color3Value", data)
	vancolor.Name = "vancolor"
	
	
	local sedan = Instance.new("IntValue", data)
	sedan.Name = "sedan"
	local sedancolor = Instance.new("Color3Value", data)
	sedancolor.Name = "sedancolor"
	
	
	local musclecar = Instance.new("IntValue", data)
	musclecar.Name = "musclecar"
	local musclecarcolor = Instance.new("Color3Value", data)
	musclecarcolor.Name = "musclecarcolor"
	
	
	local suv = Instance.new("IntValue", data)
	suv.Name = "suv"
	local suvcolor = Instance.new("Color3Value", data)
	suvcolor.Name = "suvcolor"
	
	
	local sportscar = Instance.new("IntValue", data)
	sportscar.Name = "sportscar"
	local sportscarcolor = Instance.new("Color3Value", data)
	sportscarcolor.Name = "sportscarcolor"
	
	
	local supercar = Instance.new("IntValue", data)
	supercar.Name = "supercar"
	local supercarcolor = Instance.new("Color3Value", data)
	supercarcolor.Name = "supercarcolor"
	
	
	local truck = Instance.new("IntValue", data)
	truck.Name = "truck"
	local truckcolor = Instance.new("Color3Value", data)
	truckcolor.Name = "truckcolor"
	
	
	local lightutilityvehicle = Instance.new("IntValue", data)
	lightutilityvehicle.Name = "lightutilityvehicle"
	local lightutilityvehiclecolor = Instance.new("Color3Value", data)
	lightutilityvehiclecolor.Name = "lightutilityvehiclecolor"
	
	
	local dunebuggy = Instance.new("IntValue", data)
	dunebuggy.Name = "dunebuggy"
	local dunebuggycolor = Instance.new("Color3Value", data)
	dunebuggycolor.Name = "dunebuggycolor"
	
	
	local stats = ds:GetAsync(player.UserId)
	
	if stats ~= nil then
		corvan.Value = stats[1]
		vans.Value = stats[3]
		sedan.Value = stats[5]
		musclecar.Value = stats[7]
		suv.Value = stats[9]
		sportscar.Value = stats[11]
		supercar.Value = stats[13]
		truck.Value = stats[15]
		lightutilityvehicle.Value = stats[17]
		dunebuggy.Value = stats[19]
		
		corvancolor.Value = stats[2]
		vancolor.Value = stats[4]
		sedancolor.Value = stats[6]
		musclecarcolor.Value = stats[8]
		suvcolor.Value = stats[10]
		sportscarcolor.Value = stats[12]
		truckcolor.Value = stats[14]
		lightutilityvehiclecolor.Value = stats[16]
		dunebuggycolor.Value = stats[18]
		supercarcolor.Value = stats[20]
	else
		corvan.Value = 0
		vans.Value = 0
		sedan.Value = 0
		musclecar.Value = 0
		suv.Value = 0
		sportscar.Value = 0
		truck.Value = 0
		lightutilityvehicle.Value = 0
		dunebuggy.Value = 0
		
		corvancolor.Value = BrickColor.new("White")
		vancolor.Value = BrickColor.new("White")
		sedancolor.Value = BrickColor.new("White")
		musclecarcolor.Value = BrickColor.new("White")
		suvcolor.Value = BrickColor.new("White")
		sportscarcolor.Value = BrickColor.new("White")
		truckcolor.Value = BrickColor.new("White")
		lightutilityvehiclecolor.Value = BrickColor.new("White")
		dunebuggycolor.Value = BrickColor.new("White")
		supercarcolor.Value = BrickColor.new("White")
		
	end
end)

game.Players.PlayerRemoving:Connect(function(player)
	local save = {}
	
	table.insert(save, player.cars.oldvan.Value)
	table.insert(save, player.cars.van.Value)
	table.insert(save, player.cars.sedan.Value)
	table.insert(save, player.cars.suv.Value)
	table.insert(save, player.cars.sportscar.Value)
	table.insert(save, player.cars.truck.Value)
	table.insert(save, player.cars.lightutilityvehicle.Value)
	table.insert(save, player.cars.dunebuggy.Value)
	
	table.insert(save, player.cars.oldvancolor.Value)
	table.insert(save, player.cars.vancolor.Value)
	table.insert(save, player.cars.sedancolor.Value)
	table.insert(save, player.cars.suvcolor.Value)
	table.insert(save, player.cars.sportscarcolor.Value)
	table.insert(save, player.cars.truckcolor.Value)
	table.insert(save, player.cars.lightutilityvehiclecolor.Value)
	table.insert(save, player.cars.dunebuggycolor.Value)
	
	ds:SetAsync(player.UserId, save)
end)

What I want the script to do is save color3 so that when the player enters the game again, after leaving, the car would for example be blue. I get two errors:

invalid argument #3 (Color3 expected, got number)

This is for line 81, which is:

		corvancolor.Value = stats[2]

Also an error at the last line:

Cannot store Array in data store. Data stores can only accept valid UTF-8 characters.

If you are storing around under 50 color3 values, storing them like this should be fine:

local ToStore = {colour.R,colour.G,colour.B}

But if you are storing hundreds of color3 values, for example: saving drawings, I recommend bitpacking.

Here are 2 tutorials on it.

I recommend the one below (by @CoderHusk)

I also have a Module that has BitPacking built into it, here is the thread about it, click Here to find out more!

Save this:

local ColorToSave = {Color.R, Color.G, Color.B} 

You can only save numbers or strings in datastores.

where do i save it?
at

corvancolor.Value = stats[2]

?

Well you can save it by saving it like any other table. You can apply the color by unpacking the table:

Color3.new(table.unpack(Color))

I made a tutorial on this, look at my featured topic

Custom userdata values need to be serialized before they are stored within DataStores as DataStores can only save/load primitive value types (strings, numbers, booleans, tables etc.) and subsequently deserialized when they are loaded from their DataStores back into the userdata values they represent, take the following for example.

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

local ProtectedCall = pcall

local function SerializeDataToSave(Object)
	if typeof(Object) == "Color3" then
		local Data = {Object.R, Object.G, Object.B}
		return Data
	end
end

local function DeserializeDataToLoad(Object)
	if type(Object) == "table" then
		return table.unpack(Object)
	end
end

local function SaveValue(Value)
	local Data = SerializeDataToSave(Value)
	local Success, Result = ProtectedCall(function()
		return DataStore:SetAsync(game.GameId, Data) --Example key.
	end)
	
	if Success then
		if Result then
			print(Result)
		end
	else
		warn(Result)
	end
end

local function LoadValue()
	local Success, Result = ProtectedCall(function()
		return DataStore:GetAsync(game.GameId) --Example key.
	end)
	
	if Success then
		if Result then
			print(Result)
			return DeserializeDataToLoad(Result)
		end
	else
		warn(Result)
	end
end

local Color = Color3.new(0.5, 0.5, 0.5) --Color3 value we want to save.
SaveValue(Color) --Save the Color3 value.
local R, G, B = LoadValue() --Load the R, G, B components of the saved Color3 value.
local NewColor = Color3.new(R, G, B) --Construct a new Color3 value with those three channels.
print(NewColor) --0.5, 0.5, 0.5

If you dont want to deal with doing all of the serializing yourself,

I have another solution! it can be done in two lines instead of alot! My module RBXLSerialize allows you to save entire instances, including a folder that holds all of the values in one string!:

local RBLXSerialize = require(script.Parent.RBLXSerialize)

local data = Instance.new("Folder", player)
data.Name = "cars"

local corvan = Instance.new("IntValue", data)
corvan.Name = "oldvan"
local corvancolor = Instance.new("Color3Value", data)
corvancolor.Name = "oldvancolor"

local vans = Instance.new("IntValue", data)
vans.Name = "van"
local vancolor = Instance.new("Color3Value", data)
vancolor.Name = "vancolor"

local sedan = Instance.new("IntValue", data)
sedan.Name = "sedan"
local sedancolor = Instance.new("Color3Value", data)
sedancolor.Name = "sedancolor"

local musclecar = Instance.new("IntValue", data)
musclecar.Name = "musclecar"
local musclecarcolor = Instance.new("Color3Value", data)
musclecarcolor.Name = "musclecarcolor"

local suv = Instance.new("IntValue", data)
suv.Name = "suv"
local suvcolor = Instance.new("Color3Value", data)
suvcolor.Name = "suvcolor"

local sportscar = Instance.new("IntValue", data)
sportscar.Name = "sportscar"
local sportscarcolor = Instance.new("Color3Value", data)
sportscarcolor.Name = "sportscarcolor"

local supercar = Instance.new("IntValue", data)
supercar.Name = "supercar"
local supercarcolor = Instance.new("Color3Value", data)
supercarcolor.Name = "supercarcolor"

local truck = Instance.new("IntValue", data)
truck.Name = "truck"
local truckcolor = Instance.new("Color3Value", data)
truckcolor.Name = "truckcolor"

local lightutilityvehicle = Instance.new("IntValue", data)
lightutilityvehicle.Name = "lightutilityvehicle"
local lightutilityvehiclecolor = Instance.new("Color3Value", data)
lightutilityvehiclecolor.Name = "lightutilityvehiclecolor"

local dunebuggy = Instance.new("IntValue", data)
dunebuggy.Name = "dunebuggy"
local dunebuggycolor = Instance.new("Color3Value", data)
dunebuggycolor.Name = "dunebuggycolor"

-- stores the entire folder in one string!
local Encoded = RBXLSerialize.Encode(data)

-- to decode
local dataFolder = RBXLSerialize.Decode(Encoded)
1 Like