Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.41 KB | None | 0 0
  1. function serialize(x, doNotJSON)
  2.     local s = {}
  3.  
  4.     if typeof(x) == "Instance" then
  5.         s.____s = "Instance"
  6.         s.x = {}
  7.        
  8.         for i,v in pairs(x:GetProperties()) do
  9.             if i ~= "Parent" then
  10.                 s.x[i] = serialize(v)
  11.             end
  12.         end
  13.  
  14.         s.c = {}
  15.         for _,v in pairs(x:GetChildren()) do
  16.             table.insert(s.c, serialize(v))
  17.         end
  18.     elseif typeof(x) == "table" then
  19.         s.____s = "table"
  20.        
  21.         local n = {}
  22.         for i,v in pairs(x) do
  23.             n[serialize(i)] = serialize(v)
  24.         end
  25.         s.x = n
  26.     else
  27.         s.____s = typeof(x)
  28.         s.x = tostring(x)
  29.     end
  30.  
  31.     return doNotJSON and s or game.HttpService:JSONEncode(s)
  32. end
  33.  
  34. function unserialize(x)
  35.     local s = x
  36.     pcall(function()
  37.         s = game.HttpService:JSONDecode(x) or s
  38.     end)
  39.  
  40.     if s and typeof(s) == "table" and s.____s and s.x then
  41.         if s.____s == "string" then
  42.             return s.x
  43.         elseif s.____s == "number" then
  44.             return tonumber(s.x)
  45.         elseif s.____s == "table" then
  46.             local n = {}
  47.             for _,v in pairs(s.x) do
  48.                 n[unserialize(_)] = unserialize(v)
  49.             end
  50.  
  51.             return n
  52.         elseif s.____s == "Instance" then
  53.             local i
  54.             pcall(function() i = Instance.new(unserialize(s.x.ClassName)) end)
  55.  
  56.             if i then
  57.                 for _,v in pairs(s.x) do
  58.                     pcall(function() i[_] = unserialize(v) end)
  59.                 end
  60.  
  61.                 for _,v in pairs(s.c) do
  62.                     local c = unserialize(v)
  63.                     if c then
  64.                         c.Parent = i
  65.                     end
  66.                 end
  67.             end
  68.  
  69.             return i
  70.         elseif s.____s == "boolean" then
  71.             return s.x == "true"
  72.         elseif s.____s == "Enums" then
  73.             return Enum
  74.         elseif s.____s == "Enum" then
  75.             return Enum[s.x]
  76.         elseif s.____s == "EnumItem" then
  77.             local x = string.split(s.x,".")
  78.             return Enum[x[2]][x[3]]
  79.         elseif s.____s == "BrickColor" then
  80.             return BrickColor.new(s.x)
  81.         elseif s.____s == "Ray" then
  82.             local x = string.split(s.x, "},")
  83.             return Ray.new(Vector3.new(unpack(string.split(x[1]:gsub(" ",""):gsub("{", ""):gsub("}", ""),","))), Vector3.new(unpack(string.split(x[2]:gsub(" ",""):gsub("{", ""):gsub("}", ""),","))))
  84.         elseif s.____s == "Region3" then
  85.             local x = string.split(s.x, ";")
  86.             local pos = CFrame.new(unpack(string.split(x[1]:gsub(" ",""):gsub("{", ""):gsub("}", ""),","))).p
  87.             local sz = Vector3.new(unpack(string.split(x[2]:gsub(" ",""):gsub("{", ""):gsub("}", ""),",")))
  88.  
  89.             local SizeOffset = sz/2
  90.             local Point1 = pos - SizeOffset
  91.             local Point2 = pos + SizeOffset
  92.             return Region3.new(Point1, Point2)
  93.         elseif s.____s == "Region3int16" then
  94.             local x = string.split(s.x, ";")
  95.             local a = CFrame.new(unpack(string.split(x[1]:gsub(" ",""):gsub("{", ""):gsub("}", ""),","))).p
  96.             local pos = Vector3int16.new(a.x,a.y,a.z)
  97.             local sz = Vector3int16.new(unpack(string.split(x[2]:gsub(" ",""):gsub("{", ""):gsub("}", ""),",")))
  98.  
  99.             local SizeOffset = sz/2
  100.             local Point1 = pos - SizeOffset
  101.             local Point2 = pos + SizeOffset
  102.             return Region3int16.new(Point1, Point2)
  103.         elseif s.____s == "UDim2" then
  104.             return UDim2.new(unpack(string.split(s.x:gsub(" ",""):gsub("{", ""):gsub("}", ""),",")))
  105.         elseif s.____s == "Region3int16" or s.____s == "Vector3int16" or s.____s == "Vector2int16" or s.____s == "Region3" or s.____s == "UDim" or s.____s == "Vector3" or s.____s == "Vector2" or s.____s == "CFrame" or s.____s == "Color3" then
  106.             return getfenv()[s.____s].new(unpack(string.split(s.x:gsub(" ",""),",")))
  107.         end
  108.     elseif typeof(x) == "table" then
  109.         local n = {}
  110.         for _,v in pairs(x) do
  111.             n[unserialize(_)] = unserialize(v)
  112.         end
  113.    
  114.         return n
  115.     end
  116.  
  117.     return x
  118. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement