Issues with applying ObjectSpace to object

I FOUND A FIX: I DO NOT KNOW WHY THE FIX WORKS SO IF YOU ARE WILLING TO SHARE INFO I WOULD LOVE THAT. THIS IS NO LONGER AN EMERGENCY. THE FIX IS SIMPLY FLIPPING THE NUMBERS THAT LIKE TO FLIP CHARGE FOR NO REASON BY MULTIPLYING BY NEGATIVE ONE.

This is a support category for asking questions about how to get something done on the Roblox websites or how to do something on Roblox applications such as Roblox Studio.

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

  2. What is the issue? Include screenshots / videos if possible!

I am trying to save CFrame information in object space, and read it when the player rejoins the game. The objective is to have a part respawn at a plot relative to a part on the plot, with position and rotation taken into consideration.

The issue is that when I save the CFrame in ObjectSpace using :GetComponents() I do not know how to apply it back to the relative part to get the object in the spot I want it relative to the relative part.

This is the information I am passing to the datastore

–use to object space, to convert and items CFrame to be relative to the object referenced
local actualCFrame = ownedItems[player][i].CFrame
local relativeCFrame = actualCFrame:ToObjectSpace(relativePart.CFrame)

				--position relative to relative part
				local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = relativeCFrame:GetComponents()

				--get dimensions
				local sizeX = ownedItems[player][i].Size.X
				local sizeY = ownedItems[player][i].Size.Y
				local sizeZ = ownedItems[player][i].Size.Z

				--information regarding thing saved
				table.insert(readTable[player], {
					
					sizeX,	--1
					sizeY,
					sizeZ,	--3
					
					x,	--4
					y,
					z,	--6
					
					R00,	--7
					R01,
					R02,	--9
					
					R10,	--10
					R11,
					R12,	--12
					
					R20,	--13
					R21,
					R22		--15
				})

This is how I am reading this information

		for i=1, table.maxn(readTable), 1 do
			
			--read the table and build the CFrame
			local relativeCFrame = CFrame.new(
				readTable[i][4], readTable[i][5], readTable[i][6],
				readTable[i][7], readTable[i][8], readTable[i][9],
				readTable[i][10], readTable[i][11], readTable[i][12],
				readTable[i][13], readTable[i][14], readTable[i][15])
			
			--assign CFrame to designated part
			local part = Instance.new("Part")
			
			--assign physical characteristics
			part.Parent = workspace
			
			part.Anchored = true
			
			part.CFrame = relativeCFrame*relativePart.CFrame
			
			part.Size = Vector3.new(readTable[i][1], readTable[i][2], readTable[i][3])
			
			table.insert(ownedItems[player], part)
		end
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

I have tried using to world space and to object space to reapply the CFrame, I have had some results (such as my current one listed here, where relativeCFrame is the components of the object space CFrame saved) part.CFrame = relativeCFrame*relativePart.CFrame which only put the object in the right spot EXACTLY HALF of the time. This problem is driving me insane. It is the only barrier keeping me from progressing development of my game. Please help.

the following image is the wrong result, happening half of the time

the following image is the correct result, only happening after relogging when wrong result occurs

also, this is the correct CFrame, when loading the information
-29.0507278, -0.19021976, 6.64500618, -0.999980986, -1.38461673e-05, -0.00616208464, -1.42346835e-05, 1, 6.30055147e-05, 0.00616208371, 6.30920331e-05, -0.999980986

and when that correct CFrame is saved, it saves this, which is the wrong CFrame:
-2.93319607, 0.189872026, 1.43535686, -0.999980986, 1.42346835e-05, 0.00616208371, 1.38461673e-05, 1, -6.30920331e-05, -0.00616208464, -6.30055147e-05, -0.999980986

I would recommend changing your code a bit to be more readable. While I worked at Roblox, I added RightVector and UpVector to CFrame so this sort of thing would be a lot easier. Someone else added CFrame.fromMatrix() later.

You can also save some space in your storage by eliminating one of the basis vectors and reconstructing it later - it’s always going to be orthogonal to the other two vectors so you really don’t need to store it.

Save your data like so:

local size = ownedItems[player][i].Size
table.insert(readTable[player], {
    size.X, size.Y, size.Z,
    relativeCFrame.Position.X, relativeCFrame.Position.Y, relativeCFrame.Position.Z,
    relativeCFrame.LookVector.X, relativeCFrame.LookVector.Y, relativeCFrame.LookVector.Z,
    relativeCFrame.UpVector.X, relativeCFrame.UpVector.Y, relativeCFrame.UpVector.Z
}

And read it back like this:

for i = 1, #readTable do
    local data = readTable[i]
    local size = Vector3.new(data[1], data[2], data[3])
    local pos = Vector3.new(data[4], data[5], data[6])
    local lookVector = Vector3.new(data[7], data[8], data[9])
    local upVector = Vector3.new(data[10], data[11], data[12])
    local rightVector = lookVector:Cross(upVector)
    local relativeCFrame = CFrame.fromMatrix(pos, rightVector, upVector, -lookVector)
    --do the rest
end

This should help you eliminate errors because things have meaningful names, and you save just a little space in your storage per object.

1 Like

Thank you so much! I will be looking further into this. Saving some information will help massively in the long run given the objective of the game.

Also a note, not sure of the cause still but specifically the components of the CFrame which were flipping from positive to negative were: y, R02, and R20. I didn’t mention this specifically earlier and I definitely should have.

I will be trying to find a better fix than flipping the values given these new readability techniques. Thank you sincerely for your super well formatted and useful response! It will genuinely benefit the game significantly, thank you.

1 Like

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