Need help with my saving system for a building tycoon

Hello, so I’m trying to make a saving system for my tycoon but it places it in the wrong place? There’s also no errors in the console.

Before I rejoin:
image
After I rejoin:

Saving Script:

local DS = game:GetService('DataStoreService')
local HS = game:GetService('HttpService')

local Storage = game:GetService('ReplicatedStorage')

local Store = DS:GetDataStore('Tycoons_V1')

type Position = { X:number,Y:number,Z:number }
type Rotation = { X:number,Y:number,Z:number }

game.Players.PlayerAdded:Connect(function(Plr)
	local Data:string?

	local success,err = pcall(function()
		Data = Store:GetAsync(Plr.UserId)
	end)

	if success and Data then
		local DecodedData:{ Structures:{{ Name:string,Position:Position,Rotation:Rotation }} }
		
		pcall(function()
			DecodedData = HS:JSONDecode(Data)
		end)
		
		if DecodedData then
			Plr:WaitForChild('tycoon')
			-- loading the tycoon
			for _, Key in DecodedData.Structures do
				local Structure:Model = Storage.Structures:FindFirstChild(Key.Name)

				if Structure then
					local pos = CFrame.new(Key.Position.X,Key.Position.Y,Key.Position.Z)
					local rot = CFrame.Angles(Key.Rotation.Z,Key.Rotation.Y,Key.Rotation.X)
					
					local Goal = (pos * rot):ToObjectSpace(Plr.tycoon.Value.Base.CFrame)
					
					local Clone = Structure:Clone()
					Clone:PivotTo(Goal)
					Clone.Parent = Plr:WaitForChild('tycoon').Value.Structures
				end
			end
		end
	else
		Plr:Kick('Could not load data, try rejoining.')
	end
end)

game.Players.PlayerRemoving:Connect(function(Plr)
	local Data:{ Structures:{{ Name:string,Position:Position,Rotation:Rotation }} } = {
		Structures = {  };
	}
	
	local Tycoon:Folder = Plr.tycoon.Value
	-- saving the tycoon
	for _, Structure:Model in Tycoon.Structures:GetChildren() do
		local cf = Structure.PrimaryPart.CFrame:ToWorldSpace(Tycoon.Base.CFrame)
		
		local p = cf.Position
		
		local rx,ry,rz = cf:ToOrientation()
		
		table.insert(Data.Structures,{ Name = Structure.Name,Position = { X = p.X,Y = p.Y,Z = p.Z },Rotation = { X = rx,Y = ry,Z = rz } })
	end
	
	Tycoon.Structures:ClearAllChildren()
	
	local success,err = pcall(function()
		local EncodedData = HS:JSONEncode(Data)
		
		Store:SetAsync(Plr.UserId,EncodedData)
	end)
	
	if not success then
		warn('Could not save data for ' .. Plr.Name .. '! Error: ' .. err)
	end
end)

Edit : Maybe I’m not using :ToWorldSpace() and :ToObjectSpace() in the correct way?

1 Like

Uh? I think you’d be right on the ToObjectSpace() thing.

local p1: Part = nil -- your tycoon plate i guess
local p2 : Part = nil -- placed object i guess
local offset = (p2.CFrame:ToObjectSpace(p1.CFrame)):Inverse()
p2.CFrame = p1.CFrame * offset -- should put p2 in the same exact place that it was in before

This is how you would get the offset/translation CFrame of an object. Don’t know what else to say.

1 Like

Thanks! It actually worked, I didn’t know you actually had to multiply them.

1 Like

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