Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.66 KB | None | 0 0
  1. -- [[ Services ]]
  2. local Players = game:GetService("Players")
  3. local DataStoreService = game:GetService("DataStoreService")
  4. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  5. local ServerScriptService = game:GetService("ServerScriptService")
  6.  
  7. -- [[ References ]]
  8. local GameConfig = ReplicatedStorage.GameConfig
  9.     local AutomaticSaving = require(GameConfig).AutomaticSaving
  10.     local ManualSaving = require(GameConfig).ManualSaving
  11.     local SaveOnExit = require(GameConfig).SaveOnExit
  12.     local ToolStorage = require(GameConfig).ToolStorage
  13.  
  14. local Stats = DataStoreService:GetDataStore("StatsDS")
  15. local Tools = DataStoreService:GetDataStore("ToolDS")
  16.  
  17. -- [[ Variables ]]
  18. local ServerTimeout = 5
  19.  
  20. --///////////////////////////////////////////////////////////////////////////////////////////////////--
  21.  
  22. local LoadData = function(plr, Level, XP, Gold, Gems, IP, EP)
  23.     pcall(function()
  24.         -- LoadData References
  25.         local GetData = Stats:GetAsync(plr.UserId)
  26.         local ActualTools = {}
  27.        
  28.         -- Stats
  29.         if GetData then
  30.             -- Main
  31.             Level.Value = GetData[1]
  32.             XP.Value = GetData[2]
  33.             Gold.Value = GetData[3]
  34.             Gems.Value = GetData[4]
  35.             -- Rebirths
  36.             IP.Value = GetData[5]
  37.             EP.Value = GetData[6]
  38.         else
  39.             -- Main
  40.             Level.Value = 1
  41.             XP.Value = tonumber(nil)
  42.             Gold.Value = tonumber(nil)
  43.             Gems.Value = tonumber(nil)
  44.             -- Rebirths
  45.             IP.Value = tonumber(nil)
  46.             EP.Value = tonumber(nil)
  47.         end
  48.        
  49.         -- Tools
  50.         if Tools:GetAsync(plr.UserId) then
  51.             for _, GameTools in pairs(ToolStorage:GetDescendants()) do
  52.                 for _, PlayerTools in pairs(Tools:GetAsync(plr.UserId)) do
  53.                     if GameTools:FindFirstChild(PlayerTools) then
  54.                         table.insert(ActualTools, PlayerTools)
  55.                     end
  56.                 end
  57.             end
  58.         end
  59.        
  60.         for _, GameTools in pairs(ToolStorage:GetDescendants()) do
  61.             for _, ActualPlayerTools in pairs(ActualTools) do
  62.                 GameTools:FindFirstChild(ActualPlayerTools):Clone().Parent = plr:WaitForChild("StarterGear")
  63.                 GameTools:FindFirstChild(ActualPlayerTools):Clone().Parent = plr:WaitForChild("Backpack")
  64.             end
  65.         end
  66.        
  67.         local DataLoadedSignal = Instance.new("ModuleScript", plr)
  68.         DataLoadedSignal.Name = "DataLoaded"
  69.        
  70.     end)
  71. end
  72.  
  73. ---------------------------------------------------------------------------
  74.  
  75. local SaveData = function(plr)
  76.     pcall(function()
  77.         -- SaveData References
  78.         local leaderstats = plr:FindFirstChild("leaderstats")
  79.         local SG = plr:WaitForChild("StarterGear")
  80.         local ActualTools = {}
  81.        
  82.         -- Stats
  83.         if leaderstats then
  84.             local Level = leaderstats:FindFirstChild("Level")
  85.             local XP = leaderstats:FindFirstChild("XP")
  86.             local Gold = leaderstats:FindFirstChild("Gold")
  87.             local Gems = leaderstats:FindFirstChild("Gems")
  88.             local IP = leaderstats:FindFirstChild("IP")
  89.             local EP = leaderstats:FindFirstChild("EP")
  90.            
  91.             if (Level and XP and Gold and Gems and IP and EP) ~= nil then
  92.                 Stats:SetAsync(plr.UserId, {Level.Value, XP.Value, Gold.Value, Gems.Value, IP.Value, EP.Value})
  93.             end
  94.         end
  95.        
  96.         -- Tools
  97.         for _, SGTools in pairs(SG:GetChildren()) do
  98.             table.insert(ActualTools, SGTools.Name)
  99.         end
  100.        
  101.         if ActualTools[1] then
  102.             Tools:SetAsync(plr.UserId,  ActualTools)
  103.         end
  104.        
  105.     end)
  106. end
  107.  
  108. ---------------------------------------------------------------------------
  109.  
  110. Players.PlayerAdded:Connect(function(plr)
  111.     -- PlayerAdded References
  112.     local CanAutoSave = ManualSaving
  113.     local PlayerGui = plr:WaitForChild("PlayerGui")
  114.    
  115.         --// leaderstats //--
  116.     local leaderstats = Instance.new("Folder", plr)
  117.     leaderstats.Name = "leaderstats"
  118.    
  119.     -- Main
  120.     local Level = Instance.new("DoubleConstrainedValue", leaderstats)
  121.     Level.Name = "Level"
  122.     Level.MaxValue = 10000000000000
  123.     Level.MinValue = 1
  124.    
  125.     local XP = Instance.new("DoubleConstrainedValue", leaderstats)
  126.     XP.Name = "XP"
  127.     XP.MaxValue = math.huge
  128.     XP.MinValue = 0
  129.    
  130.     local Gold = Instance.new("DoubleConstrainedValue", leaderstats)
  131.     Gold.Name = "Gold"
  132.     Gold.MaxValue = math.huge
  133.     Gold.MinValue = 0
  134.    
  135.     local Gems = Instance.new("DoubleConstrainedValue", leaderstats)
  136.     Gems.Name = "Gems"
  137.     Gems.MaxValue = math.huge
  138.     Gems.MinValue = 0
  139.    
  140.     -- Rebirths
  141.     local IP = Instance.new("DoubleConstrainedValue", leaderstats)
  142.     IP.Name = "IP"
  143.     IP.MaxValue = math.huge
  144.     IP.MinValue = 0
  145.    
  146.     local EP = Instance.new("DoubleConstrainedValue", leaderstats)
  147.     EP.Name = "EP"
  148.     EP.MaxValue = math.huge
  149.     EP.MinValue = 0
  150.    
  151.     --------------------------------------------------
  152.    
  153.     wait()
  154.     pcall(function()
  155.         LoadData(plr, Level, XP, Gold, Gems, IP, EP)
  156.     end)
  157.    
  158.     --------------------------------------------------
  159.    
  160.     -- Manual Saving
  161.     if ManualSaving then
  162.         plr.Chatted:Connect(function(ChatSendedMessage)
  163.             if (ChatSendedMessage == "/save") or (ChatSendedMessage == ";save") then
  164.                 if CanAutoSave then
  165.                     CanAutoSave = false
  166.                     SaveData(plr)
  167.                     warn("PlayerData for ".. plr.Name .." Has been Saved.")
  168.                     wait(3 * 60)
  169.                     CanAutoSave = true
  170.                 elseif (not CanAutoSave) then
  171.                     warn("PlayerData for ".. plr.Name .." Can't be Saved.")
  172.                 end
  173.             end
  174.         end)
  175.     end
  176.    
  177.     -- Automatic Saving
  178.     if AutomaticSaving then
  179.         local C = nil
  180.        
  181.         if (not AutomaticSaving) then
  182.             C = 10
  183.         else
  184.             C = 5
  185.         end
  186.        
  187.         while wait(C * 60) do
  188.             if plr then
  189.                 pcall(function()
  190.                     SaveData(plr)
  191.                 end)
  192.             else
  193.                 break
  194.             end
  195.         end
  196.     end
  197.    
  198. end)
  199.  
  200. ---------------------------------------------------------------------------
  201.  
  202. Players.PlayerRemoving:Connect(function(plr)
  203.     if (not plr) then return end
  204.     pcall(function()
  205.         if SaveOnExit then
  206.             SaveData(plr)
  207.         end
  208.     end)
  209. end)
  210.  
  211. game:BindToClose(function()
  212.     for _, GamePlayers in pairs(Players:GetPlayers()) do
  213.         if GamePlayers then
  214.             SaveData(GamePlayers)
  215.         end
  216.     end
  217.     wait(ServerTimeout)
  218. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement