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