Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --// Services \\--
- local Players = game:GetService("Players")
- local ReplicatedStorage = game:GetService("ReplicatedStorage")
- local DataStoreService = game:GetService("DataStoreService")
- local RunService = game:GetService("RunService")
- --// Modules \\--
- local Settings = require(script.Settings)
- local Datastore = DataStoreService:GetDataStore("Datastore #----")
- --// Tables \\--
- local Starting_Data = {
- Data = {
- Stats = {
- Strength = "BEEP BEEP",
- },
- },
- }
- local Servers_Data = {}
- local Servers_Log = {}
- --// Functions \\--
- local function Data_Attempt(Player, Function, Argument)
- local Success, Error, Attempt, Data = nil, nil, 0, nil
- while Attempt < Settings.MaxAttempts and not Success do
- Attempt = Attempt + 1
- Success, Error = pcall(function()
- Data = Function(Player, Argument)
- end)
- wait()
- end
- --[[repeat wait()
- Attempt = Attempt + 1
- Success, Error = pcall(function()
- Data = Function(Player, Argument)
- end)
- until Success or Attempt >= Settings.MaxAttempts do end
- --]]
- if Success then
- print("[Database] The function: |"..Function.."| has successfully been called for the player: |"..Player.Name.."|")
- else
- warn("[Database] The function: |"..Function.."| has unsuccessfully been called for the player: |"..Player.Name.."|")
- end
- return Data
- end
- local function Save(Player, Data)
- Datastore:SetAsync(Player.UserId, Data)
- end
- local function GetData(Player)
- local Data = Datastore:GetAsync(Player.UserId)
- return Data
- end
- --// Modules 2 \\--
- local module = {
- NewPlayer = function(Player)
- local Players_Data = Data_Attempt(Player, GetData)
- if Players_Data == nil then
- Servers_Data[Player].Data = Starting_Data.Data
- print("[Database] The player: |"..Player.Name.."| had no pre-existing data, they have been awarded the default data.")
- elseif Players_Data then
- Servers_Data[Player].Data = Players_Data
- print("[Database] The player: |"..Player.Name.."| had pre-existing data, they have been awarded their last data save.")
- end
- end
- }
- --// Player Control \\--
- Players.PlayerAdded:Connect(function(Player)
- Servers_Data[Player] = {}
- if RunService:IsStudio() and not Settings.StudioLoad then return end
- module.NewPlayer(Player)
- end)
- Players.PlayerRemoving:Connect(function(Player)
- if RunService:IsStudio() and not Settings.StudioSave then return end
- Data_Attempt(Player, Save, Servers_Data[Player].Data)
- print("[Database] The player: |"..Player.Name.."| has left the game.")
- end)
- game:BindToClose(function()
- if RunService:IsStudio() and not Settings.StudioSave then return end
- local Player_List = Players:GetPlayers()
- for i = 1,#Player_List do
- local Player = Player_List[i]
- print("[Database] The player: |"..Player.Name.."| is having their data safely saved.")
- Data_Attempt(Player, Save, Servers_Data[Player].Data)
- end
- end)
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement