The script below is part of a multiscript system that generates an infinite number of ordered blocks (neighborhood blocks like a group of neighborhood homes). The specific script below is intended to save things about each home and load the home and all of it’s items with it. Each home is customizable by the player like in Bloxburg and therefore each object they place in/on their home will be saved with it’s position and orientation.
The problem is that whenever saving the location and orientation of each part it seems to always give nil. I’m not exactly sure if it’s just not saving the position and orientation or if it’s returning it in a different format.
I’ve rewritten the whole script a few times and this is the most optimized version I’ve written so far. I’ve add lots of prints around the script to help track how far along the path the system gets before something goes wrong.
Here is the script, it’s a module script in the server script service:
local HttpService = game:GetService("HttpService")
local Datastore = game:GetService("DataStoreService")
local homeData = Datastore:GetDataStore("homeData")
local playerData = Datastore:GetDataStore("playerData")
local HomeLoader = {}
function HomeLoader.SaveHome(block, owner, index, Home)
print("Saving home "..block..":"..index.."...")
local HomeData = { Owner = tonumber(owner), Objects = {} }
for _, child in pairs(Home:GetChildren()) do
table.insert(HomeData.Objects, {
Name = child.Name,
Position = child.Position,
Orientation = child.Orientation
})
end
homeData:SetAsync(block..":"..index, HttpService:JSONEncode(HomeData))
print("Home "..block..":"..index.." saved!")
end
function LoadHome(block, i, player)
print("Loading Home...")
local _HomeData = homeData:GetAsync(block..":"..i)
if _HomeData then
local HomeData = HttpService:JSONDecode(_HomeData)
local NewHome = game.ReplicatedStorage.Home:Clone()
print("Home loaded!")
NewHome.Name = tostring(block..":"..i)
for _, Block in pairs(workspace.Blocks:GetChildren()) do
if block == Block.Name then
NewHome.Parent = Block.Homes
NewHome:PivotTo(CFrame.new(Block.Road:WaitForChild(i).Position.X, 0, Block.Road:WaitForChild(i).Position.Z))
break
end
end
if HomeData.Objects then
for _, value in pairs(HomeData.Objects) do
print(value.Name)
local object = game.ReplicatedStorage.Objects:FindFirstChild(value.Name):Clone()
object.Parent = NewHome
object.Name = value.Name
object.Position = value.Position
object.Orientation = value.Orientation
end
end
for _, child in pairs(NewHome:GetChildren()) do
child.Transparency = 1
local info = TweenInfo.new(3, Enum.EasingStyle.Exponential)
local goal = { Transparency = 0 }
local Tween = game:GetService("TweenService"):Create(child, info, goal)
Tween:Play()
end
-- Connect the ChildAdded event to save the home
NewHome.ChildAdded:Connect(function(child)
HomeLoader.SaveHome(block, player.UserId, i, NewHome)
end)
HomeLoader.SaveHome(block,player.UserId,i,NewHome)
else
local NewHome = game.ReplicatedStorage.DefaultHome:Clone()
print("Home loaded!")
NewHome.Name = tostring(block..":"..i)
for _, Block in pairs(workspace.Blocks:GetChildren()) do
if block == Block.Name then
NewHome.Parent = Block.Homes
NewHome:PivotTo(CFrame.new(Block.Road:WaitForChild(i).Position.X, 0, Block.Road:WaitForChild(i).Position.Z))
break
end
end
for _, child in pairs(NewHome:GetChildren()) do
child.Transparency = 1
local info = TweenInfo.new(3, Enum.EasingStyle.Exponential)
local goal = { Transparency = 0 }
local Tween = game:GetService("TweenService"):Create(child, info, goal)
Tween:Play()
end
-- Connect the ChildAdded event to save the home
NewHome.ChildAdded:Connect(function(child)
HomeLoader.SaveHome(block, player.UserId, i, NewHome)
end)
HomeLoader.SaveHome(block,player.UserId,i,NewHome)
end
end
function HomeLoader.LoadHomes(block)
for i = 1, 4 do
local _HomeData = homeData:GetAsync(block..":"..i)
if _HomeData then
print("Home found for "..block..":"..i.."!")
local HomeData = HttpService:JSONDecode(_HomeData)
LoadHome(block, i, HomeData.Owner)
else
print("No home found for "..block..":"..i.."!")
local ProxPrompt = Instance.new("ProximityPrompt")
for _, Block in pairs(workspace.Blocks:GetChildren()) do
if block == Block.Name then
ProxPrompt.Parent = Block.Road:FindFirstChild(i)
end
end
ProxPrompt.Triggered:Connect(function(player)
if player.leaderstats.Silver.Value >= 2000 and player.Homes.Value < 1 then
player.leaderstats.Silver.Value -= 2000
player.Homes.Value += 1
ProxPrompt.Enabled = false
LoadHome(block, i, player)
end
end)
end
end
end
return HomeLoader