Need help with a build save and load system

I’m working on a save and load system for my game, :cd::electric_plug: mod simulator (discord mod simulator) and rotated objects keep appearing in weird spots when I reload the game.

here’s a video:

here’s the code for the save and load system:

local prompt = script.Parent
local claim = prompt.Parent
local plot = claim.Parent
local house = plot.House

local DSS = game:GetService("DataStoreService")
local Data = DSS:GetDataStore("dms-data")

local HTTP = game:GetService("HttpService")

local RS = game:GetService("ReplicatedStorage")
local FurnitureObjects = RS:WaitForChild("FurnitureObjects")
local Events = RS:WaitForChild("Events")

local HandleComputer_Folder = Events:WaitForChild("HandleComputer")
local HandleComputer = {
	Remote = HandleComputer_Folder:WaitForChild("Remote"), 
	Bindable = HandleComputer_Folder:WaitForChild("Bindable")
}

function LoadDataWithKey(key)

	local data = nil
	local success, response = pcall(function()

		data = Data:GetAsync(key)

	end)

	if success then

		return data

	elseif not success then

		warn(response)
		return "fail"

	end

end

function SaveDataWithKey(data, key)

	local success, response = pcall(function()

		Data:UpdateAsync(key, function()

			return data

		end)

	end)

	if success then

		return true

	else

		warn(response)
		return false

	end

end

prompt.Triggered:Connect(function(plr)
	
	local data = plr:WaitForChild("data")
	local House = data:WaitForChild("House")
	
	local loadedHouse = LoadDataWithKey(plr.UserId.."-house")
	if loadedHouse == "fail" then warn("failed to load house") return end
	
	if plot.Owner.Value or House.Value then return end
	
	House.Value = house
	plot.Owner.Value = plr
	
	claim.Transparency = 1
	claim.CanCollide = false
	prompt.Enabled = false
	--
	for i, furnitureInfo in pairs(HTTP:JSONDecode(loadedHouse)) do
		
		local furniture = FurnitureObjects:FindFirstChild(furnitureInfo.Name)
		print(furnitureInfo)
		if furniture then
			
			local CFrameTable = furnitureInfo.CFrame
			local CF = CFrame.new(table.unpack(CFrameTable.Position)) * CFrame.Angles(math.rad(CFrameTable.Angles[1]), math.rad(CFrameTable.Angles[2]), math.rad(CFrameTable.Angles[3]))
			local newFurniture = furniture:Clone()
			newFurniture.Parent = house.Placed
			newFurniture:PivotTo(CF:ToWorldSpace(house:GetPivot()))
			
			if furniture:FindFirstChild("ComputerIndicator") then
				
				HandleComputer.Remote:FireClient(plr, newFurniture)
				
			end
			
		end
		
	end
	
	game.Players.PlayerRemoving:Connect(function(plr_)
		
		if plr_ == plr then
			
			local data = {}

			claim.Transparency = 0
			claim.CanCollide = true
			prompt.Enabled = true
			plot.Owner.Value = nil

			-- SAVE HOUSE

			for i, furniture:Model in pairs(house.Placed:GetChildren()) do
				
				local pos = house:GetPivot():ToObjectSpace(furniture:GetPivot()).Position
				local rotX, rotY, rotZ = furniture:GetPivot():ToEulerAnglesXYZ()
				data[#data+1] = {
					["Name"] = furniture.Name, 
					["CFrame"] = {
						Position = {
							pos.X, pos.Y, pos.Z
						}, 
						Angles = {
							math.deg(rotX), math.deg(rotY), math.deg(rotZ)
						}
					}
				}

			end

			print(data)
			local res = SaveDataWithKey(HTTP:JSONEncode(data), plr.UserId.."-house")
			print(if res then "saved successfully" else "failed to save")
			
		end
		
	end)
	
end)

here’s the place file:
discordmodsimulator.rbxl (283.3 KB)

1 Like

In your saving function, try

local houseSpaceFurniturePivot = house:GetPivot():ToObjectSpace(furniture:GetPivot())
local rotX, rotY, rotZ = houseSpaceFurniturePivot:ToEulerAnglesXYZ()

… so you properly account for the rotation of the house, just like with the position.

A:ToWorldSpace(B) doesn’t convert A to world space, it converts B to world space from A’s space. It’s the same with ToObjectSpace BTW. Try

newFurniture:PivotTo(house:GetPivot():ToWorldSpace(CF))

… to transform CF from house space to world space.

If that doesn’t work, I can never remember how to properly reconstruct CFrame angles from pitch, yaw, roll. So maybe it’s not working properly. I’d try using the 12 components of the CFrame because the order of values returned from GetComponents is the same as for the 12-parameter CFrame constructor.

thank you so much!!
it worked :smiley:

1 Like

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