Can't store CFrame to datastore

I am trying to make a saving furniture placement system and everything works flawlessly except for saving the data. For some reason I keep getting this error:

the print above the error is an example of one of the cframes and I have a feeling it’s because of those weird ‘e-07’ numbers

code:

local a = item:GetPivot()
local cf = workspace.Map.Houses[plr.Name].Grid.CFrame:toObjectSpace(a)
table.insert(data.House, {[item.Name] = cf})
1 Like

CFrame is not UTF8, you must stringify it and then convert it back to a CFrame object when you need to use it, this is called serialization/deserialization.

2 Likes

You can’t directly write a CFrame (or other userdata type) to a Datastore, nor can you write a table directly that’s not a pure array.

You can get around storing a dictionary by using HttpService:JSONEncode() and JSONDecode() to encode the table as a blob of JSON (which is just a string). This alone is not enough to store a CFrame though, any CFrame objects in the table will just store as the string “null”. You can convert the CFrame to an array of Lua-native Numbers though and store that:

Instead of
{[item.Name] = cf}

you can so something like:
{[item.Name] = table.pack(cf:GetComponents())}

You can devise your own way to serialize and deserialize a CFrame to Numbers as well, if you want to optimize it (e.g. by storing only non-default entries or storing as quaternion components, etc.) The important thing is that what you’re storing is implicitly convertable to and from a string. Scientfic notation numbers should not be a problem, tonumber() can handle these just fine.

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.