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:
(in the middle is what the encoded data looks like)
I’m sorry this is so specific and I’m probably missing something stupid.