How could I edit my datastore to allow for Color3 / Vector3 savings?

Awhile back I worked on a DS module with the help of this forum - So far it factors for all the values shown below & saves them. I was wondering how / if I could implement a method of saving {all} Color3 values and / or all Vector3 values within the set folder.

	local System = {}
	
	local DataStoreService = game:GetService("DataStoreService")
	local DSS = DataStoreService:GetDataStore("DataStoreService", 0.01)
	
	function System.DataSave(Player)
		local success, Data = pcall(function()
			return DSS:SetAsync(Player.UserId, saveToDictionary(Player["PlayerData"]))
		end)
		if success then 
			print("Saved!") 
		else 
			print("Failed!") 
		end
	end
	
	function System.DataLoad(Player)
		local success, Data = pcall(function()
			return DSS:GetAsync(Player.UserId)
		end)
		if success then
			if Data then
				print("Old Data Loaded")
				local PlayerDataFolder = materializeDictionary(Data)
				PlayerDataFolder.Name = "PlayerData"
				PlayerDataFolder.Parent = Player
			else
				print("New Data Loaded")
				local PlayerDataFolder = script["Template"]:Clone()
				PlayerDataFolder.Name = "PlayerData"
				PlayerDataFolder.Parent = Player
				
				System.DataSave(Player)
			end
		end
	end
	
	function saveToDictionary(object)
		if not object:IsA("Configuration") then
			return object.Value
		else
			local newTable = {}
			for i,v in pairs(object:GetChildren()) do
				newTable[v.Name] = saveToDictionary(v)
			end
			return newTable
		end
	end
	
	function materializeDictionary(input)
		if typeof(input) == "table" then
			local newFolder = Instance.new("Configuration")
			for i,v in pairs(input) do
				local newObject = materializeDictionary(v)
				newObject.Name = i
				newObject.Parent = newFolder
			end
			return newFolder
		elseif typeof(input) == "number" then
			local newIntValue = Instance.new("NumberValue")
			newIntValue.Value = input
			return newIntValue
		elseif typeof(input) == "string" then
			local newStringValue = Instance.new("StringValue")
			newStringValue.Value = input
			return newStringValue
		elseif typeof(input) == "boolean" then
			local newBoolValue = Instance.new("BoolValue")
			newBoolValue.Value = input
			return newBoolValue
		elseif typeof(input) == "boolean" then
			local newBoolValue = Instance.new("BoolValue")
			newBoolValue.Value = input
			return newBoolValue
		end
	end
	
	return System

Convert them to string/table (whatever you prefer).

-- Vector3 and Color3 to string
("%s; %s; %s"):format(v3.X, v3.Y, x3.Z)
("%s; %s; %s"):format(c3.R, c3.G, c3.B)
-- Vector3 and Color3 to array
{ v3.X, v3.Y, v3.Z }
{ c3.R, c3.G, c3.B }

It’s calling serialising, this might help you:

2 Likes

When you are saving a Vector3 save a table like this:

{Vector.X,Vector.Y,Vector.Z}

And when you are loading it just do this:

Vector = Vector3.new(Data.X,Data.Y,Data.Z)

The same with Color3.

1 Like

I just created one on quick with more dimensions in one table, here is an instance:

local StringVector = tostring(workspace.Vector.Value)
local StringColor = tostring(workspace.Color.Value)

local TableVector = string.split(StringVector,", ")
local TableColor = string.split(StringColor,", ")

local SaveTable = {
	Vector = {
		X = tonumber(TableVector[1]),
		Y = tonumber(TableVector[2]),
		Z = tonumber(TableVector[3])
	},
	
	Color = {
		R = tonumber(TableColor[1]),
		G = tonumber(TableColor[2]),
		B = tonumber(TableColor[3])
	}
}
-------------------------------------------------
for i,v in pairs(SaveTable.Vector) do
	print(v)
end

for i,v in pairs(SaveTable.Color) do
	print(v)
end
3 Likes

I receive an error from this method:
[ServerScriptService.Script.DataStore:32: bad argument #2 (base out of range)](rbxopenscript://www.dummy.com/dummy?
scriptGuid=%7BD60589A4%2DDA66%2D4CAA%2D83FC%2DC55C8164A7F6%7D&gst=3#32)

Edit: My bad - I was forgetting the “toNumber”! Thank you!!