Guest User

Untitled

a guest
Mar 30th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.22 KB | None | 0 0
  1. -- services --
  2.  
  3. local repStorage = game:GetService("ReplicatedStorage")
  4. local httpService = game:GetService("HttpService")
  5.  
  6. -- other variables --
  7.  
  8. local ds
  9. local lastSave
  10. local matShort = { -- short material names --
  11.     Brick = {"Brick","B"},
  12.     Cobblestone = {"Cobblestone","CS"},
  13.     Concrete = {"Concrete","CC"},
  14.     CorrodedMetal = {"CorrodedMetal","CM"},
  15.     DiamondPlate = {"DiamondPlate","DP"},
  16.     Fabric = {"Fabric","FC"},
  17.     Foil = {"Foil","FL"},
  18.     Glass = {"Glass","GL"},--[[ET]]
  19.     Granite = {"Granite","GR"},
  20.     Grass = {"Grass","GS"},
  21.     Ice = {"Ice","I"},
  22.     Marble = {"Marble","MR"},
  23.     Metal = {"Metal","M"},
  24.     Neon = {"Neon","N"},
  25.     Pebble = {"Pebble","PE"},
  26.     Plastic = {"Plastic","P"},
  27.     Sand = {"Sand","SA"},
  28.     Slate = {"Slate","SL"},
  29.     SmoothPlastic = {"SmoothPlastic","SP"},
  30.     Wood = {"Wood","W"},
  31.     WoodPlanks = {"WoodPlanks","WP"}
  32. }
  33.  
  34. -- main code (this will only show up for scripts that are importiant and have a lot of lines of code) --
  35.  
  36. ds = game:GetService("DataStoreService")
  37. lastSave = ds:GetDataStore("LastSave")
  38.  
  39. function save(plr,time)
  40.     plr.PlrInfo.Loading.Value = true
  41.     local mTable = {}
  42.     local tables = {}
  43.     local t = {}
  44.     for i,v in pairs(workspace.PlayerModels:GetChildren()) do -- save statue needs to be implimented
  45.         if v:FindFirstChild("Owner") then
  46.             if v.Owner.Value == plr.Name then
  47.                 table.insert(t,v)
  48.             end
  49.         end
  50.     end
  51.     if #t ~= 0 and plr.PlrInfo.Loaded.Value then
  52.         local a = 0
  53.         for i,v in pairs(t) do
  54.             a = a + 1
  55.             if time and a == 10 then
  56.                 a = 0
  57.                 game:GetService("RunService").Heartbeat:Wait()
  58.             end
  59.             repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(i/#t,0,1,0))
  60.             if v:FindFirstChild("Owner") then
  61.                 if v.Owner.Value == plr.Name then
  62.                     if #mTable > 1100 then -- impliment table size; not number of structures
  63.                         table.insert(tables,mTable)
  64.                         print("Table Too Big, Added to tables. TableNumber: "..#tables)
  65.                         mTable = {}
  66.                     end
  67.                     local Table = {}
  68.                     if v.Name == "Radio" then
  69.                         Table.ID = v[v.Name]
  70.                     end
  71.                     Table.Name = v.Name
  72.                     Table.CFrame = {(v:GetPrimaryPartCFrame():ToObjectSpace(plr.PlotOwned.Value.TP.CFrame)):components()}
  73.                     Table.PartC = tostring(v.PrimaryPart.BrickColor)
  74.                     if v:FindFirstChild("CanChangeMaterial") then
  75.                         Table.PartM = matShort[string.sub(tostring(v.Main.Material), 15)][2]
  76.                     end
  77.                     if v.PrimaryPart then
  78.                         v:Destroy()
  79.                     end
  80.                     table.insert(mTable, Table)
  81.                 end
  82.             else
  83.                 print("Oof")
  84.             end
  85.         end
  86.         table.insert(tables, mTable)
  87.     end
  88.    
  89.     for i,v in pairs(tables) do
  90.         pcall(function()
  91.             if typeof(v) ~= "table" and #v == 0 then
  92.                 table.remove(tables,i)
  93.             end
  94.         end)
  95.     end
  96.     print(#tables)
  97.     ds:GetDataStore("PlotSaveInfo"..plr.PlrInfo.SlotLoaded.Value):SetAsync(plr.UserId, #tables)
  98.     repStorage.Events.ChangeSlotLoadingText:FireClient(plr, "Saving To Datastores; 0/"..#tables)
  99.     repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(0,0,1,0))
  100.     for i = 1,#tables do
  101.         print(string.len(httpService:JSONEncode(tables[i])))
  102.         ds:GetDataStore("PlotSave"..plr.PlrInfo.SlotLoaded.Value):SetAsync(plr.UserId+i, tables[i])
  103.         repStorage.Events.ChangeSlotLoadingText:FireClient(plr, "Saving To Datastores; "..i.."/"..#tables)
  104.         repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(i/#tables,0,1,0))
  105.         print("Saved Data To DataStore; "..i.."/"..#tables)
  106.     end
  107.     wait(0.4)
  108.     plr.PlrInfo.Loading.Value = false
  109.     plr.PlrInfo.Loaded.Value = false
  110.     repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(0,0,1,0))
  111.     return false
  112. end
  113.  
  114. function repStorage.Functions.SaveLoad.Save.OnServerInvoke(plr)
  115.     return save(plr,true)
  116. end
  117.  
  118. function repStorage.Functions.SaveLoad.Load.OnServerInvoke(plr)
  119.     local t = {}
  120.     for i,v in pairs(workspace.PlayerModels:GetChildren()) do
  121.         if v:FindFirstChild("Owner") then
  122.             if v.Owner.Value == plr.Name then
  123.                 table.insert(t,v)
  124.             end
  125.         end
  126.     end
  127.     for i,v in pairs(t) do
  128.         game:GetService("RunService").Heartbeat:Wait()
  129.         if v:FindFirstChild("Owner") then
  130.             if v.Owner.Value == plr.Name then
  131.                 v:Destroy()
  132.             end
  133.         end
  134.     end
  135.     print("Fetching Save")
  136.     if ds:GetDataStore("PlotSaveInfo"..plr.PlrInfo.SlotLoaded.Value):GetAsync(plr.UserId) or nil then
  137.         repStorage.Events.ChangeSlotLoadingText:FireClient(plr, "Loading Slot "..plr.PlrInfo.SlotLoaded.Value)
  138.         local bits = ds:GetDataStore("PlotSaveInfo"..plr.PlrInfo.SlotLoaded.Value)
  139.         local a = 0
  140.         for i = 1,bits:GetAsync(plr.UserId) do
  141.             repStorage.Events.ChangeSlotLoadingText:FireClient(plr, "Loading Slot "..plr.PlrInfo.SlotLoaded.Value.."(Data Bit:"..i.."/"..bits:GetAsync(plr.UserId)..")")
  142.             local sTable = ds:GetDataStore("PlotSave"..plr.PlrInfo.SlotLoaded.Value):GetAsync(plr.UserId+i)
  143.             for i,v in pairs(sTable) do
  144.                 a = a + 1
  145.                 if a == 10 then
  146.                     a = 0
  147.                     game:GetService("RunService").Heartbeat:Wait()
  148.                 end
  149.                 repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(i/#sTable,0,1,0))
  150.                 if repStorage.Place:FindFirstChild(v.Name) then
  151.                     local obj = repStorage.Place[v.Name]:Clone()
  152.                     obj.Parent = workspace.PlayerModels
  153.                     obj:SetPrimaryPartCFrame(plr.PlotOwned.Value.TP.CFrame * CFrame.new(unpack(v.CFrame)):Inverse())
  154.                     local name = Instance.new("StringValue", obj)
  155.                     name.Name = "Owner"
  156.                     name.Value = plr.Name
  157.                     if v.Name == "Radio" then
  158.                         obj.Radio.TabletScreen.Music.SoundId = v.ID
  159.                     end
  160.                     if v.PartC then
  161.                         obj.Main.BrickColor = BrickColor.new(v.PartC)
  162.                     end
  163.                     if v.PartM then
  164.                         for x,b in pairs(matShort) do
  165.                             if b[2] == v.PartM then
  166.                                 obj.Main.Material = b[1]
  167.                             end
  168.                         end
  169.                     end
  170.                     if obj:FindFirstChild(obj.name) then
  171.                         for _,a in pairs(obj[obj.Name]:GetChildren()) do
  172.                             if a:IsA("BasePart") then
  173.                                 for x,b in pairs(matShort) do
  174.                                     if b[2] == v.PartM then
  175.                                         a.Material = b[1]
  176.                                     end
  177.                                 end
  178.                             end
  179.                         end
  180.                     end
  181.                 end            
  182.             end
  183.         end
  184.     end
  185.     wait(0.4)
  186.     print("Load Success")
  187.     repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(0,0,1,0))
  188.     plr.PlrInfo.Loaded.Value = true
  189.     return false -- for gui
  190. end
  191.  
  192. game:BindToClose(function()
  193.     for i,v in pairs(game.Players:GetPlayers()) do
  194.         if v:FindFirstChild("PlotOwned") and v.PlrInfo.SlotLoaded.Value ~= -1 then
  195.             print("Saving "..v.Name)
  196.             save(v,false)
  197.         end
  198.     end
  199. end)
Advertisement
Add Comment
Please, Sign In to add comment