Hello everyone.
I am trying to save what each player has changed in his slot (similar to Lumber Tycoon 2), but when saving the objects that each player added in his house, they are not saved because they are models and cannot be serialized.
Script
Credits to @valchip for creating this uncopylocked script.
local DSS = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local serverDSS = DSS:GetDataStore("server_DSS_x1bdnfh4")
local Players = game:GetService("Players")
local loaded = false
local Workspace = {}
local function updateDictionary()
Workspace = {}
for i, v in pairs(workspace:GetChildren()) do
if v.Name ~= "Camera" and v.Name ~= "Terrain" and v.Name ~= "Baseplate" and v:IsA("Part") then
for _, plr in pairs (game:GetService("Players"):GetPlayers()) do
if plr.Name == v.Name and v:IsA("Model") and v:FindFirstChild("Humanoid") ~= nil then
return warn("Can't decode.")
end
end
local a = {}
a.Position = {v.Position.X, v.Position.Y, v.Position.Z}
a.Size = {v.Size.X, v.Size.Y, v.Size.Z}
a.Name = {v.Name}
a.Anchored = {v.Anchored}
a.ClassName = {v.ClassName}
a.Orientation = {v.Orientation.X, v.Orientation.Y, v.Orientation.Z}
a.CanCollide = {v.CanCollide}
a.Color = {math.floor((v.Color.r)*255), math.floor((v.Color.g)*255), math.floor((v.Color.b)*255)}
a.Material = {v.Material.Name}
a.Shape = {v.Shape.Name}
table.insert(Workspace, a)
end
end
dataTOsave = HttpService:JSONEncode(Workspace)
end
function DecodeDictionary(dic)
local inst = nil
for i, v in pairs(dic) do
game:GetService('RunService').Heartbeat:Wait()
for a, b in pairs(v) do
if a == "ClassName" then
if b[1] ~= "Part" then
return
end
inst = Instance.new(b[1])
inst.Parent = workspace
end
end
for a, b in pairs(v) do
if a == "Size" then
inst.Size = Vector3.new(b[1], b[2], b[3])
elseif a == "Position" then
inst.Position = Vector3.new(b[1],b[2],b[3])
elseif a == "Name" then
inst.Name = b[1]
elseif a == "Anchored" then
inst.Anchored = b[1]
elseif a == "Orientation" then
inst.Orientation = Vector3.new(b[1],b[2],b[3])
elseif a == "CanCollide" then
inst.CanCollide = b[1]
elseif a == "Color" then
inst.Color = Color3.fromRGB(b[1], b[2], b[3])
elseif a == "Shape" then
inst.Shape = b[1]
elseif a == "Material" then
inst.Material = b[1]
end
end
end
end
local JSONdata = serverDSS:GetAsync("server_key")
if JSONdata ~= nil and JSONdata ~= "[]" then
local data = HttpService:JSONDecode(JSONdata)
DecodeDictionary(data)
loaded = true
print("loaded")
else
loaded = true
end
game:BindToClose(function()
updateDictionary()
wait()
if loaded == true then
print(#dataTOsave)
serverDSS:SetAsync("server_key", dataTOsave)
end
end)
Any solution, or something I can do to make this work well?
Some help?
Thanks in advance.
Happy programming!
Regards, nanitook.