Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- services --
- local repStorage = game:GetService("ReplicatedStorage")
- local httpService = game:GetService("HttpService")
- -- other variables --
- local ds
- local lastSave
- local matShort = { -- short material names --
- Brick = {"Brick","B"},
- Cobblestone = {"Cobblestone","CS"},
- Concrete = {"Concrete","CC"},
- CorrodedMetal = {"CorrodedMetal","CM"},
- DiamondPlate = {"DiamondPlate","DP"},
- Fabric = {"Fabric","FC"},
- Foil = {"Foil","FL"},
- Glass = {"Glass","GL"},--[[ET]]
- Granite = {"Granite","GR"},
- Grass = {"Grass","GS"},
- Ice = {"Ice","I"},
- Marble = {"Marble","MR"},
- Metal = {"Metal","M"},
- Neon = {"Neon","N"},
- Pebble = {"Pebble","PE"},
- Plastic = {"Plastic","P"},
- Sand = {"Sand","SA"},
- Slate = {"Slate","SL"},
- SmoothPlastic = {"SmoothPlastic","SP"},
- Wood = {"Wood","W"},
- WoodPlanks = {"WoodPlanks","WP"}
- }
- -- main code (this will only show up for scripts that are importiant and have a lot of lines of code) --
- ds = game:GetService("DataStoreService")
- lastSave = ds:GetDataStore("LastSave")
- function save(plr,time)
- plr.PlrInfo.Loading.Value = true
- local mTable = {}
- local tables = {}
- local t = {}
- for i,v in pairs(workspace.PlayerModels:GetChildren()) do -- save statue needs to be implimented
- if v:FindFirstChild("Owner") then
- if v.Owner.Value == plr.Name then
- table.insert(t,v)
- end
- end
- end
- if #t ~= 0 and plr.PlrInfo.Loaded.Value then
- local a = 0
- for i,v in pairs(t) do
- a = a + 1
- if time and a == 10 then
- a = 0
- game:GetService("RunService").Heartbeat:Wait()
- end
- repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(i/#t,0,1,0))
- if v:FindFirstChild("Owner") then
- if v.Owner.Value == plr.Name then
- if #mTable > 1100 then -- impliment table size; not number of structures
- table.insert(tables,mTable)
- print("Table Too Big, Added to tables. TableNumber: "..#tables)
- mTable = {}
- end
- local Table = {}
- if v.Name == "Radio" then
- Table.ID = v[v.Name]
- end
- Table.Name = v.Name
- Table.CFrame = {(v:GetPrimaryPartCFrame():ToObjectSpace(plr.PlotOwned.Value.TP.CFrame)):components()}
- Table.PartC = tostring(v.PrimaryPart.BrickColor)
- if v:FindFirstChild("CanChangeMaterial") then
- Table.PartM = matShort[string.sub(tostring(v.Main.Material), 15)][2]
- end
- if v.PrimaryPart then
- v:Destroy()
- end
- table.insert(mTable, Table)
- end
- else
- print("Oof")
- end
- end
- table.insert(tables, mTable)
- end
- for i,v in pairs(tables) do
- pcall(function()
- if typeof(v) ~= "table" and #v == 0 then
- table.remove(tables,i)
- end
- end)
- end
- print(#tables)
- ds:GetDataStore("PlotSaveInfo"..plr.PlrInfo.SlotLoaded.Value):SetAsync(plr.UserId, #tables)
- repStorage.Events.ChangeSlotLoadingText:FireClient(plr, "Saving To Datastores; 0/"..#tables)
- repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(0,0,1,0))
- for i = 1,#tables do
- print(string.len(httpService:JSONEncode(tables[i])))
- ds:GetDataStore("PlotSave"..plr.PlrInfo.SlotLoaded.Value):SetAsync(plr.UserId+i, tables[i])
- repStorage.Events.ChangeSlotLoadingText:FireClient(plr, "Saving To Datastores; "..i.."/"..#tables)
- repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(i/#tables,0,1,0))
- print("Saved Data To DataStore; "..i.."/"..#tables)
- end
- wait(0.4)
- plr.PlrInfo.Loading.Value = false
- plr.PlrInfo.Loaded.Value = false
- repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(0,0,1,0))
- return false
- end
- function repStorage.Functions.SaveLoad.Save.OnServerInvoke(plr)
- return save(plr,true)
- end
- function repStorage.Functions.SaveLoad.Load.OnServerInvoke(plr)
- local t = {}
- for i,v in pairs(workspace.PlayerModels:GetChildren()) do
- if v:FindFirstChild("Owner") then
- if v.Owner.Value == plr.Name then
- table.insert(t,v)
- end
- end
- end
- for i,v in pairs(t) do
- game:GetService("RunService").Heartbeat:Wait()
- if v:FindFirstChild("Owner") then
- if v.Owner.Value == plr.Name then
- v:Destroy()
- end
- end
- end
- print("Fetching Save")
- if ds:GetDataStore("PlotSaveInfo"..plr.PlrInfo.SlotLoaded.Value):GetAsync(plr.UserId) or nil then
- repStorage.Events.ChangeSlotLoadingText:FireClient(plr, "Loading Slot "..plr.PlrInfo.SlotLoaded.Value)
- local bits = ds:GetDataStore("PlotSaveInfo"..plr.PlrInfo.SlotLoaded.Value)
- local a = 0
- for i = 1,bits:GetAsync(plr.UserId) do
- repStorage.Events.ChangeSlotLoadingText:FireClient(plr, "Loading Slot "..plr.PlrInfo.SlotLoaded.Value.."(Data Bit:"..i.."/"..bits:GetAsync(plr.UserId)..")")
- local sTable = ds:GetDataStore("PlotSave"..plr.PlrInfo.SlotLoaded.Value):GetAsync(plr.UserId+i)
- for i,v in pairs(sTable) do
- a = a + 1
- if a == 10 then
- a = 0
- game:GetService("RunService").Heartbeat:Wait()
- end
- repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(i/#sTable,0,1,0))
- if repStorage.Place:FindFirstChild(v.Name) then
- local obj = repStorage.Place[v.Name]:Clone()
- obj.Parent = workspace.PlayerModels
- obj:SetPrimaryPartCFrame(plr.PlotOwned.Value.TP.CFrame * CFrame.new(unpack(v.CFrame)):Inverse())
- local name = Instance.new("StringValue", obj)
- name.Name = "Owner"
- name.Value = plr.Name
- if v.Name == "Radio" then
- obj.Radio.TabletScreen.Music.SoundId = v.ID
- end
- if v.PartC then
- obj.Main.BrickColor = BrickColor.new(v.PartC)
- end
- if v.PartM then
- for x,b in pairs(matShort) do
- if b[2] == v.PartM then
- obj.Main.Material = b[1]
- end
- end
- end
- if obj:FindFirstChild(obj.name) then
- for _,a in pairs(obj[obj.Name]:GetChildren()) do
- if a:IsA("BasePart") then
- for x,b in pairs(matShort) do
- if b[2] == v.PartM then
- a.Material = b[1]
- end
- end
- end
- end
- end
- end
- end
- end
- end
- wait(0.4)
- print("Load Success")
- repStorage.Events.ChangePercent:FireClient(plr, UDim2.new(0,0,1,0))
- plr.PlrInfo.Loaded.Value = true
- return false -- for gui
- end
- game:BindToClose(function()
- for i,v in pairs(game.Players:GetPlayers()) do
- if v:FindFirstChild("PlotOwned") and v.PlrInfo.SlotLoaded.Value ~= -1 then
- print("Saving "..v.Name)
- save(v,false)
- end
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment