Unable to populate CFrame values with variables

  1. What do you want to achieve? Keep it simple and clear!
    I’d like to copy cframe values from a saved database and apply them to the currentcamera

  2. What is the issue? Include screenshots / videos if possible!
    Any way I have tried applying the CFrame values, they always turn into 0’s … my latest attempt is below.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Much iterative sweat and many pleas to Google :pray: :sob:

The CFrame is retrieved in string format from the Datastore and looks like this:

location = "181.934998, 31.7661476, -178.768829, 0.914765835, 0, 0.403984576, 0, 1, 0, -0.403984576, 0, 0.914765835"

I turn that into a variable called ‘coordinates’ and then split it like this:

local coordinate_parts = string.split(location, " ")

then I populate the CFrame on the currentcamera like so:

camera.CFrame =  CFrame.new(coordinate_parts[1],coordinate_parts[2],coordinate_parts[3],coordinate_parts[4],coordinate_parts[5],coordinate_parts[6],coordinate_parts[7],coordinate_parts[8],coordinate_parts[9],coordinate_parts[10],coordinate_parts[11],coordinate_parts[12])

testing the .split like this:

print(coordinate_parts[1]) -- expected value is returned

The problem: the currentcamera.CFrame is always (0,0,0,0,0,0,0,0,0,0,0,0)

Thanks!!!

Try wrapping each parameter of CFrame.new in a tonumber().

2 Likes

That’s because all your coordinates include a comma.

Also, if saving to data store, I would recommend a binary format to save a lot of space.
Serializing:

serialized = string.pack("<ffffffffffff", cf:GetComponents())

Deserializing:

deserialized = CFrame.new(string.unpack("<ffffffffffff", saved))
2 Likes

The first step is to set the CameraType to Scriptable, as this allows scripts to edit the camera:

camera.CameraType = Enum.CameraType.Scriptable

The second step is to split the saved string correctly:

local savedString = "1, 2, 3, 4"

-->> what you're currently doing
local coordinate_parts = string.split(savedString, " ") --> "1,", "2,", "3,", "4,"

-->> what you need to do
local coordinate_parts = string.split(savedString, ", ") --> "1", "2", "3", "4"

Quick tip. You don’t need to index each key in coordinate_parts, you can use table.unpack instead:

local cf = CFrame.new(table.unpack(coordinate_parts))

table.unpack basically splits an array into a tuple.
Here’s a visualization:

local str = "1, 2, 3, 4"
local splitString = string.split(savedString, ", ") --> "1", "2", "3", "4"
local a, b, c, d = table.unpack(splitString)
print(a, b, c, d) --> "1 2 3 4"
2 Likes

:heart_hands:

Thank you. Please feel free to get in touch if you think I could ever help you.

Thank you. This is a great suggestion :ok_hand:

Please feel free to get in touch if you think I could ever help you with anything.

:ok_hand:

Thank you! Get in touch if I can ever help.

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