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:
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?