Saving a wrong CFrame with ToWorldSpace

Im making one of those tycoons where there are Multiple plots and you can build anywhere in that plot.
The issue im having is related to the way im saving the props that the player has built

As you can see in the video, even though i dont change the location of the prop i placed, the script saves its CFrame rotated by 90 degrees and in a different position, the loaded and saved CFrames printed in the output are also different.

These are the save/load codes


I would appreciate any help.

1 Like

What I did when i was creating plots before would be when a player is exiting I subtract the plots x and y. For example in code:

for i, model in ipairs(models:GetChildren()) do
			local pivot = model:GetPivot()
			local X,Y,Z = model:GetPivot():ToOrientation()
			table.insert(data, {
				model.Name,
				pivot.Position.X - actualBase.Position.X,
				pivot.Position.Y - actualBase.Position.Y,
				pivot.Position.Z - actualBase.Position.Z,
				math.deg(Y)
			})
		end

then when it would be loaded, I would add it back

finalCFrame = newPart:PivotTo(CFrame.new(
				obj[2] + actualBase.Position.X,
				obj[3] + actualBase.Position.Y,
				obj[4] + actualBase.Position.Z
				))

then for saving deg I would just do

math.deg(Y) -- Y is the part model instance

then when you load it I would do

local rotatedCFrame = pivot * CFrame.fromEulerAngles(
				0,
				math.rad(obj[5]),
				0
			)
			newPart:PivotTo(rotatedCFrame)
3 Likes

Try saving this way

local objCFrame = v.CFrame
local plot = workspace.Baseplates[Player.Name]
local offset = objCFrame:inverse() * (plot.CFrame)
Props[i] = {
    Name = v.Name;
    CFrameComponents = { offset:GetComponents() }
}

workspace.Bases[Player.Name]:Destroy()

You can then load them by just multiplying the offset by the plot CFrame.

local unpacked = CFrame.new(table.unpack(v.CFrameComponents))
Prop:PivotTo(Baseplate.CFrame * unpacked + Vector3.new(0, plot.CFrame.Y - unpacked.Y, 0))

Edit: Fixed a few tiny things after testing. This should work, as long as the CFrames aren’t too manipulated.

Proof /w Testing

Results:

Code used for testing:

local objCFrame = workspace:WaitForChild("PartToReplicate").CFrame
local plot = workspace:WaitForChild("Plot0")

local plotCFrame = plot.CFrame
local offset = objCFrame:Inverse() * (plot.CFrame)
local CFrameComponents = { offset:GetComponents() }

local newObj = workspace:WaitForChild("PartToReplicate"):Clone()
newObj:PivotTo(workspace:WaitForChild("Plot1").CFrame * CFrame.new(table.unpack(CFrameComponents)) + Vector3.new(0, workspace.Plot1.Position.Y - offset.Y, 0))
newObj.Parent = workspace

Hierarchy:
image

Before Running:

1 Like

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