Plot saving system not rotating loaded models properly

Hello,
I am currently working on a building system for my game.

I made a test script below that serializes and encodes a baseplate’s models and then loads them to another baseplate (with a different position and rotation)

local folder = game.Workspace.testsave --i put the test models here, they are also in replicatedstorage.PlaceableModels
local baseplate = game.Workspace.baseplate

local BaseData = {}
for i, v in pairs(folder:GetChildren()) do
	if v:IsA("Model") then
		local cf = baseplate.CFrame:ToObjectSpace(v.PrimaryPart.CFrame) 
		--local cf = baseplate.CFrame:Inverse() * v.PrimaryPart.CFrame
	
		local look = cf.LookVector
		print(tostring(look))
		local pos = cf.Position
		local posx = pos.X
		local posy = pos.Y
		local posz = pos.Z
		local lookx = look.X
		local looky = look.Y
		local lookz = look.Z
		local modelData = {v.Name,posx,posy,posz,lookx,looky,lookz}
		table.insert(BaseData,modelData)
	end
end
local DataString = game:GetService("HttpService"):JSONEncode(BaseData)
print(DataString)

wait(1)

local BaseData = game:GetService("HttpService"):JSONDecode(DataString)
for i, data in pairs(BaseData) do
	local name = data[1]
	local pos = Vector3.new(data[2],data[3],data[4])
	local look = Vector3.new(data[5],data[6],data[7])
	local model = game.Workspace.testsave:FindFirstChild(name)
	
	local cf = workspace.baseplate2.CFrame:ToWorldSpace(CFrame.new(pos,look))
	
	
	if model then
		local clone = model:Clone()
		clone.Parent = workspace
		clone:SetPrimaryPartCFrame(cf)--This almost wrks, the position is correct but the rotation is always off!
		print(tostring(cf.LookVector))
		
	else
		table.remove(BaseData,i)
	end
end

This almost works. It properly saves everything, and then loads the models. However, their rotation is off. Some how the position works but the rotation doesn’t. Here’s what it’s saving:

and here’s what it loads:

(these are two different baseplates)

I tried switching the object and world space functions, as well as the origin and CFrame. The current script is as close to accurate as I could get. I can see that it’s the rotation that’s not working. The print functions print the lookvectors, and here’s what the output looks like: image
(in the middle is what the encoded data looks like)

I’m sorry this is so specific and I’m probably missing something stupid.

1 Like

Have you tried setting the Orientation?

1 Like

No, but I will consider that. I suppose I can just subtract the orientation of the baseplate from the model.

1 Like

I will mark this as the solution for now. I got it to work by changing to the following:

local folder = game.Workspace.testsave --i put the test models here, they are also in replicatedstorage.PlaceableModels
local baseplate = game.Workspace.baseplate

local BaseData = {}
for i, v in pairs(folder:GetChildren()) do
	if v:IsA("Model") then
		local pos = baseplate.CFrame:PointToObjectSpace(v.PrimaryPart.CFrame.Position) 
		--local cf = baseplate.CFrame:Inverse() * v.PrimaryPart.CFrame
	
		local look = v.PrimaryPart.Orientation - baseplate.Orientation
		print(tostring(look))
		--local pos = cf.Position
		local posx = pos.X
		local posy = pos.Y
		local posz = pos.Z
		local lookx = look.X
		local looky = look.Y
		local lookz = look.Z
		local modelData = {v.Name,posx,posy,posz,lookx,looky,lookz}
		table.insert(BaseData,modelData)
	end
end
local DataString = game:GetService("HttpService"):JSONEncode(BaseData)
print(DataString)

wait(1)

local BaseData = game:GetService("HttpService"):JSONDecode(DataString)
for i, data in pairs(BaseData) do
	local name = data[1]
	local pos = Vector3.new(data[2],data[3],data[4])
	local look = Vector3.new(data[5],data[6],data[7])
	local model = game.Workspace.testsave:FindFirstChild(name)
	
	local cf = workspace.baseplate2.CFrame:ToWorldSpace(CFrame.new(pos))
	
	
	
	if model then
		local newlook = look-- + workspace.baseplate2.Orientation
		local clone = model:Clone()
		clone.Parent = workspace
		clone:SetPrimaryPartCFrame(cf)--This almost wrks, the position is correct but the rotation is always off!
		clone:SetPrimaryPartCFrame(clone.PrimaryPart.CFrame:ToWorldSpace(CFrame.Angles(math.rad(newlook.X),math.rad(newlook.Y),math.rad(newlook.Z))))
		print(tostring(cf.LookVector))
		
	else
		table.remove(BaseData,i)
	end
end

There’s almost definitely a better way to do this, but since it works I’m going to use this until/unless I learn something better.
note: there’s some weird commented out stuff in this. I don’t understand why I didn’t have to add the orientation of baseplate 2, but I didn’t and it worked.

1 Like