Help Needed with CFrames

I am converting a :toObjectSpace() to a :toWorldSpace() and have created this function to do so, however, it keeps erroring this error and I’m not sure why:

invalid argument #1 to 'new' (Vector3 expected, got string)

Here is my code:

local function loadObjectToWorldCF(target, storedKey)
-- storedKey is a string
-- target is a part inside of workspace

	print(storedKey)
	local world = target.CFrame:toWorldSpace(CFrame.new(storedKey))

	workspace.CurrentCamera.Focus = CFrame.new(target.Position)
	workspace.CurrentCamera.CFrame = world
end

Output:

1.53450012, 0.229022503, -0.182975769, -0.167098686, -0.217205867, 0.96171701, -3.72529074e-09, 0.975431442, 0.220303282, -0.985940218, 0.0368123837, -0.162993312 
invalid argument #1 to 'new' (Vector3 expected, got string)

Any help is appreciated, thank you!

2 Likes

This is the error line. You’re trying to making a string to become a CFrame.

1 Like

The CFrame is being stored in a StringValue, so when I go to retrieve the CFrame, I say stringvalue.Value, which will give me a string. How can I convert this into a CFrame that CFrame.new() can understand?

1 Like

There is no way that you can convert a whole string to that, however we can serialise it.

The way how we do that is make 3 IntValues containing the the CFrame’s first 3 value, AKA the X, Y and Z coordinate. Then to serialise it:

XValue = CFrame.X
YValue = CFrame.Y
ZValue = CFrame.Z

Then to deserialise it(AKA load the CFrame):

part.CFrame.X = XValue.Value
part.CFrame.Y = YValue.Value
part.CFrame.Z = ZValue.Value

I hope this work, since it’s my first time to serialise things after I learnt serialization in Roblox.

2 Likes

I think I can make this work, thank you!

1 Like

I think what you are looking for is a CFrameValue or a Vector3Value.

2 Likes

This will not work, as you can’t save instances through DataStores.

1 Like

CFrameValue.Value is a CFrame.new() object. Which I can think you can save to DataStore, it was what @0929lego was doing, but correct me if I am wrong.

You can save the value but not the instance.

Edit: Sorry I didn’t realize you meant the value of the instance and not the actual CFrameValue instance itself.

1 Like

If that doesn’t work, have you tried tonumber(stringvalue.Value)?

That will error because there are commas in the string, for example: “1,0,3,6,3,4,1,6,3,5,2,3”.

That’s because that’s a CFrameValue array not a Vector3 array.

If you want a Vector3 array out of it then you use the tonumber function result with.X, Y, and/or Z.

I’m using a CFrame value array because it is a camera CFrame so it needs orientation and all the other values.

In that case it would work for CFrame.new because it’s a CFrame array. Have you at least tried it before saying it won’t work?

I’ve tried it with a similar array (“10,20,40,30”) and it returned nil but I will try it again and see if it works. Thank you!