DataStore Col3?

I’m currently working on a Datastore that works by serialising the contents of a Folder;
I’m currently struggling to get the DataStore to recognise, saved / load Color3 values?
I’ve tried quite a few methods however nothing successfully works.

I always receive this: error: Unable to assign property Value. Color3 expected, got table.

Does anyone have any ideas?

Script

local Core = { ... }
local Server, Self = game, script

local Properties = {
	Configuration = {'Name'},
	Folder = {'Name'},
	StringValue = {'Name', 'Value'},
	NumberValue = {'Name', 'Value'},
	Color3Value = {'Name', 'Value'}
}

function Serialize(Prop, ...)
	local type = typeof(Prop)
	local Value
	if type == 'BrickColor' then
		Value = tostring(Prop)
	elseif type == 'CFrame' then
		Value = {Pos = Serialize(Prop.Position), rX = Serialize(Prop.rightVector), rY = Serialize(Prop.upVector), rZ = Serialize(-Prop.lookVector)}
	elseif type == 'Color3' or type == 'Color' then	
		Value = {R = math.floor((Prop.R*255)+0.5), G = math.floor((Prop.G*255)+0.5), B = math.floor((Prop.B*255)+0.5)}		
	elseif type == 'Vector3' then
		Value = {X = Prop.X, Y = Prop.Y, Z = Prop.Z}
	elseif type == 'EnumItem' then
		Value = {string.split(tostring(Value), ".")[2], string.split(tostring(Value), ".")[3]}
	else
		Value = Prop
	end
	return Value
end

function Deserialize(type, Prop, ...)
	local Value
	if type == 'Position' or type == 'Size' then
		Value = Vector3.new(Prop.X, Prop.Y, Prop.Z)
	elseif type == 'CFrame' then
		Value = CFrame.fromMatrix(Deserialize('Position', Prop.pos), Deserialize('Position', Prop.rX), Deserialize('Position', Prop.rY), Deserialize('Position', Prop.rZ))
	elseif type == 'BrickColor' then
		Value = BrickColor.new(Prop)
	elseif type == 'Color3' or type == 'Color' then
		Value = Color3.fromRGB(Prop.R, Prop.G, Prop.B)
		return Value
	elseif type == 'Material' or type == 'Face' or type == 'Shape' then
		Value = Enum[Prop[1][Prop][2]]
	else
		Value = Prop
	end
	return Value
end

function InitProps(object, ...)
	local tableToSave = {}
	for __, Obj in (object) do
		local Class = Obj.ClassName
		local t = tableToSave[Class]
		if not (t) then
			tableToSave[Class] = {}
			t = tableToSave[Class]
		end
		local Add = {}
		for __, Prop in (Properties[Obj.ClassName]) do
			Add[Prop] = Serialize(Obj[Prop], ...)
		end
		local Children = Obj:GetChildren(...)
		if #Children > 0 then
			Add['Children'] = InitProps(Children, ...)
		end
		table.insert(t, Add)
	end
	return tableToSave
end

function Create(Parent, t)
	for Class, __ in (t) do
		for __, Obj in (t[Class]) do
			local Object = Instance.new(Class)
			for Prop, Value in (Obj) do
				if Prop ~= 'Children' then
					Object[Prop] = Deserialize(Prop, Value)
				else
					Create(Object, Value)
				end
			end
			Object.Parent = Parent
		end
	end
end

function Core.Encrypt(object, ...)
	return Server:GetService('HttpService'):JSONEncode(InitProps(object), ...)
end

function Core.Decrypt(Dictionary, Slot, ...)
	local t = Server:GetService('HttpService'):JSONDecode(Dictionary)
	Create(Slot, t)
end

return Core

1 Like

Datastore does not like color3 values, i’ve never been able to save them. Try having 3 values for each.

1 Like

I’ve had a version in the past that allowed me to do it as it’s literally just a table of the 3 values, however for some reason it just won’t de-serialize it.

1 Like

Save it as a hex string instead

Convert the Color3 to a string using toHex
Convert the string to a Color3 using fromHex

2 Likes

Close but now I just receive this error:
Unable to assign property Value. Color3 expected, got string

To convert the string back to a Color3 use Color3.fromHex

1 Like

That’s what I have done :confused:

elseif type == 'Color3' or type == 'Color' or type == 'Color3Value' then
			Value = Color3.fromHex(Prop)

Not completely sure of the context of the script but if the type of the value is a color3, then toHex will turn the color3 into a string. It looks like you’re checking if the value is a color3, then trying to convert the color3 into a color3 using fromHex, which would be wrong

1 Like