Trying to save a player's house

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.

1 Like

try to put .Name on the a letter like this

table.insert(Workspace, a.Name)

you could just loop the players house and the contents inside it

then save the contents on the table and

when they leave save it
on the table

when they join find their contents on the table

if it were findable then do FindFirstChild(a)

if something was found in the a table then find the name of the content and load it

Is there an error? If so which line is it? Or is it not saving or is it not loading?

On second look, you also missed the ‘n’ of Orientation.
a.Orientatio = {v.Orientation.X, v.Orientation.Y, v.Orientation.Z}
a.Orientation it should be.
This line is in your updateDictionary function.

1 Like

Yes, the error I am having is as it is programmed in the script: return warn("Can't decode")

I think the problem is that being a model, it does not have the properties of a part, so it cannot be decoded. Somehow I have to make it detect if it is a model, look for the parts within the model, and save it that way. But I’m not sure how to do it.

You should use workspace:GetDescendants() in that case.
It will loop through EVERY single instance in the workspace, including parts inside models.
I think you’d also want to store each model as a separate key with an indicator inside to check if it is a model or not.
If v:IsA(“Model”), create another loop that recurses through the model till no other children are found.

3 Likes