I’m working on a save and load system for my game, 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)