Alright so I have seen quite a few people requesting data deserializers and instance serializers so I am here to show you a simple derserializer&serializer I made. I am providing steps on how it works and script. The deserializer&serializer only saves parts, and only 5 properties of them, their size, position, class name, name and if the part is anchored or not. It automatically “saves the workspace” when the game gets shut down. I do realize that if there were to be 2 servers running for example one of the servers would lose data I am doing this just to show how you would go about making a derserializer&serializer. This was mainly made to give an idea on how a deserializer and serializer works.
Here is how it works:
Game runs.
Game gets the data of the game from the datastore, a JSON encoded dictionary.
Data gets JSON decoded into a dictionary.
Data (the dictionary) then gets deserialized by my own deserializer.
For every new child added or removed the dictionary gets the elements of the workspace and their properties and adds them in the dictionary, currently it only gets 5 properties, does not save the baseplate, camera, the terrain or the players’ character. Basically here the serialization process occurs.
Game shuts down.
The dictionary gets JSON encoded.
The dictionary gets saved in the datastore.
So yeah. Here is the script for that:
--//Data Deserializer And Instance Serializer
--//Scripted by vamik64
local DSS = game:GetService("DataStoreService")
local HttpService = game:GetService("HttpService")
local serverDSS = DSS:GetDataStore("serverDSS_1")
local Players = game:GetService("Players")
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
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}
table.insert(Workspace, a)
end
end
dataTOsave = HttpService:JSONEncode(Workspace)
end
function DecodeDictionary(dic)
local inst = nil
for i, v in pairs(dic) do
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
print("Decoded "..inst.Name)
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]
end
end
end
end
local JSONdata = serverDSS:GetAsync("server_key")
if JSONdata ~= nil and JSONdata ~= "[]" then
local data = HttpService:JSONDecode(JSONdata)
DecodeDictionary(data)
end
workspace.ChildAdded:Connect(function()
updateDictionary()
end)
workspace.ChildRemoved:Connect(function()
updateDictionary()
end)
game:BindToClose(function()
print("JSON encoded dictionary:\r "..dataTOsave)
serverDSS:SetAsync("server_key", dataTOsave)
end)
The lines of code I have written can easily be reduced. I might not be using the most efficient method but here is how I made mine! Also this is my first contribution to the DevForum’s community . I am open to/accept criticism so if you want to criticize me, give me ideas, give me your opinion I would love to hear it!