Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- [[ Services ]]
- local Players = game:GetService("Players")
- local DataStoreService = game:GetService("DataStoreService")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local ServerScriptService = game:GetService("ServerScriptService")
- -- [[ References ]]
- local GameConfig = ReplicatedStorage.GameConfig
- local AutomaticSaving = require(GameConfig).AutomaticSaving
- local ManualSaving = require(GameConfig).ManualSaving
- local SaveOnExit = require(GameConfig).SaveOnExit
- local ToolStorage = require(GameConfig).ToolStorage
- local Stats = DataStoreService:GetDataStore("StatsDS")
- local Tools = DataStoreService:GetDataStore("ToolDS")
- -- [[ Variables ]]
- local ServerTimeout = 5
- --///////////////////////////////////////////////////////////////////////////////////////////////////--
- local LoadData = function(plr, Level, XP, Gold, Gems, IP, EP)
- pcall(function()
- -- LoadData References
- local GetData = Stats:GetAsync(plr.UserId)
- local ActualTools = {}
- -- Stats
- if GetData then
- -- Main
- Level.Value = GetData[1]
- XP.Value = GetData[2]
- Gold.Value = GetData[3]
- Gems.Value = GetData[4]
- -- Rebirths
- IP.Value = GetData[5]
- EP.Value = GetData[6]
- else
- -- Main
- Level.Value = 1
- XP.Value = tonumber(nil)
- Gold.Value = tonumber(nil)
- Gems.Value = tonumber(nil)
- -- Rebirths
- IP.Value = tonumber(nil)
- EP.Value = tonumber(nil)
- end
- -- Tools
- if Tools:GetAsync(plr.UserId) then
- for _, GameTools in pairs(ToolStorage:GetDescendants()) do
- for _, PlayerTools in pairs(Tools:GetAsync(plr.UserId)) do
- if GameTools:FindFirstChild(PlayerTools) then
- table.insert(ActualTools, PlayerTools)
- end
- end
- end
- end
- for _, GameTools in pairs(ToolStorage:GetDescendants()) do
- for _, ActualPlayerTools in pairs(ActualTools) do
- GameTools:FindFirstChild(ActualPlayerTools):Clone().Parent = plr:WaitForChild("StarterGear")
- GameTools:FindFirstChild(ActualPlayerTools):Clone().Parent = plr:WaitForChild("Backpack")
- end
- end
- local DataLoadedSignal = Instance.new("ModuleScript", plr)
- DataLoadedSignal.Name = "DataLoaded"
- end)
- end
- ---------------------------------------------------------------------------
- local SaveData = function(plr)
- pcall(function()
- -- SaveData References
- local leaderstats = plr:FindFirstChild("leaderstats")
- local SG = plr:WaitForChild("StarterGear")
- local ActualTools = {}
- -- Stats
- if leaderstats then
- local Level = leaderstats:FindFirstChild("Level")
- local XP = leaderstats:FindFirstChild("XP")
- local Gold = leaderstats:FindFirstChild("Gold")
- local Gems = leaderstats:FindFirstChild("Gems")
- local IP = leaderstats:FindFirstChild("IP")
- local EP = leaderstats:FindFirstChild("EP")
- if (Level and XP and Gold and Gems and IP and EP) ~= nil then
- Stats:SetAsync(plr.UserId, {Level.Value, XP.Value, Gold.Value, Gems.Value, IP.Value, EP.Value})
- end
- end
- -- Tools
- for _, SGTools in pairs(SG:GetChildren()) do
- table.insert(ActualTools, SGTools.Name)
- end
- if ActualTools[1] then
- Tools:SetAsync(plr.UserId, ActualTools)
- end
- end)
- end
- ---------------------------------------------------------------------------
- Players.PlayerAdded:Connect(function(plr)
- -- PlayerAdded References
- local CanAutoSave = ManualSaving
- local PlayerGui = plr:WaitForChild("PlayerGui")
- --// leaderstats //--
- local leaderstats = Instance.new("Folder", plr)
- leaderstats.Name = "leaderstats"
- -- Main
- local Level = Instance.new("DoubleConstrainedValue", leaderstats)
- Level.Name = "Level"
- Level.MaxValue = 10000000000000
- Level.MinValue = 1
- local XP = Instance.new("DoubleConstrainedValue", leaderstats)
- XP.Name = "XP"
- XP.MaxValue = math.huge
- XP.MinValue = 0
- local Gold = Instance.new("DoubleConstrainedValue", leaderstats)
- Gold.Name = "Gold"
- Gold.MaxValue = math.huge
- Gold.MinValue = 0
- local Gems = Instance.new("DoubleConstrainedValue", leaderstats)
- Gems.Name = "Gems"
- Gems.MaxValue = math.huge
- Gems.MinValue = 0
- -- Rebirths
- local IP = Instance.new("DoubleConstrainedValue", leaderstats)
- IP.Name = "IP"
- IP.MaxValue = math.huge
- IP.MinValue = 0
- local EP = Instance.new("DoubleConstrainedValue", leaderstats)
- EP.Name = "EP"
- EP.MaxValue = math.huge
- EP.MinValue = 0
- --------------------------------------------------
- wait()
- pcall(function()
- LoadData(plr, Level, XP, Gold, Gems, IP, EP)
- end)
- --------------------------------------------------
- -- Manual Saving
- if ManualSaving then
- plr.Chatted:Connect(function(ChatSendedMessage)
- if (ChatSendedMessage == "/save") or (ChatSendedMessage == ";save") then
- if CanAutoSave then
- CanAutoSave = false
- SaveData(plr)
- warn("PlayerData for ".. plr.Name .." Has been Saved.")
- wait(3 * 60)
- CanAutoSave = true
- elseif (not CanAutoSave) then
- warn("PlayerData for ".. plr.Name .." Can't be Saved.")
- end
- end
- end)
- end
- -- Automatic Saving
- if AutomaticSaving then
- local C = nil
- if (not AutomaticSaving) then
- C = 10
- else
- C = 5
- end
- while wait(C * 60) do
- if plr then
- pcall(function()
- SaveData(plr)
- end)
- else
- break
- end
- end
- end
- end)
- ---------------------------------------------------------------------------
- Players.PlayerRemoving:Connect(function(plr)
- if (not plr) then return end
- pcall(function()
- if SaveOnExit then
- SaveData(plr)
- end
- end)
- end)
- game:BindToClose(function()
- for _, GamePlayers in pairs(Players:GetPlayers()) do
- if GamePlayers then
- SaveData(GamePlayers)
- end
- end
- wait(ServerTimeout)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement