DataStore and CFrame storing

I am currently working on a multi-place game and am wondering: How the heck do I use DataStores to store part CFrames?

  1. What kind of values can be saved, can tables be saved?

  2. How would go about serializing my CFrame values?

My save system is going to be similar to Build a Boat for Treasure where each individual part loads up with it’s own CFrames.

5 Likes

I think saving a table with DataStore is possible, and table can contain CFrames.

2 Likes

You could try using something like this:

local cf = CFrame.new(0,50,0);

function cframeToTable(cf)
	return {cf:GetComponents()};
end

function tableToCframe(t)
	return CFrame.new(table.unpack(t));
end

-- Both print 50.
print(cframeToTable(cf)[2]);
print(tableToCframe(cframeToTable(cf)).p.Y);
20 Likes

The script below will add your cframes to a table. All you have to do is datastore it yourself.
The reason why I didn’t datastore it is because I didn’t learn datastoring yet.

local CFrameData = {}

local DataStoreService = game:GetService("DataStoreService")
local Data = DataStoreService:GetDataStore('Data')

function AddCFrameToTable(Table,Parts)
	for _, v in pairs(Parts:GetDescendants()) do
		if v:IsA('BasePart') then
		table.insert(Table,#Table+1,v.CFrame.Position)
		end
	end
end
function PrintTable(Table)
	for _, v in pairs(Table)do
		print(v)
	end
end



AddCFrameToTable(CFrameData,workspace.Parts)
wait(1.040542562456)
PrintTable(CFrameData)

Just as with Color3 and Instances, CFrame is a UserData value and cannot be directly saved to a DataStore.

I’d suggest using @Nucl3arPlays’ method for serializing (turning into a table that you can then store) and deserializing (converting into an actual CFrame)

3 Likes

I believe JSON encoding can encode any type of value aside from instances (i.e CFrame, Color3, Vector3, UDim2, etc).

JSON encoding is useless for datastores, as to actually use the data you need to deserialise it back to a table. You can store tables in datastores.

That is what I’m doing right now (storing tables) and it’s working. I only have to build a reconstruction mechanism now.

There are compression modules that allow you to compress string, meaning if you JSON Encoded a table, then compressed it, it would compress into the datastore under its character limit. I wouldn’t call it useless since you would be able to compress it and store it successfully.

That only applies if you’re storing data which may exceed the size limit— but that is not the case when storing CFrames.

I still JSON Encode because I do not know how big my tables can be and it could most likely corrupt due to the table not fitting in a certain character limit. Plus you can only store color3 values in JSON Encoded tables, you’d have to store color3 values as numbers in a table for a datastore. But, you do you.

The same problems you raised for tables are applicable to JSONEncode. JSONEncode bloats the amount of characters you use, thus expanding the size required to fit into a DataStore. Malformed or otherwise unacceptable characters will result in JSONEncode failing and thus can be “corrupted”.

You should store Color3 values in their individual RGB values, not the raw datatype - again, thats more bloat to your DataStore unnecessarily and in addition you’re now sending over an unsupported datatype to DataStore servers.

1 Like

Is there any way to add more “cframe values” to the cf variable. I was attempting to convert a table then convert that table into regular values using table.concat and separating them with commas but that could not work because I believe it does not support values. Also, I tried defaultios module but it has many inaccuracies and incorrectly converts the cframe position by a long shot.