Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local Players = game:GetService("Players")
- local RunService = game:GetService("RunService")
- local DataStoreService = game:GetService("DataStoreService")
- local MainStatsDatastore = DataStoreService:GetDataStore("MainStats") -- GearsData
- local function waitForRequestBudget(requestType)
- local currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
- while currentBudget < 1 do
- currentBudget = DataStoreService:GetRequestBudgetForRequestType(requestType)
- task.wait(5)
- end
- end
- game.Players.PlayerAdded:Connect(function(Player)
- if Player then
- local leaderstats = Instance.new("Folder",Player)
- leaderstats.Name = "leaderstats"
- local Wins = Instance.new("IntValue",leaderstats)
- Wins.Name = "Wins"
- local Coins = Instance.new("IntValue",leaderstats)
- Coins.Name = "Coins"
- local Gems = Instance.new("IntValue",leaderstats)
- Gems.Name = "Gems 💎"
- local ValuesFolder = Instance.new("Folder",Player)
- ValuesFolder.Name = "ValuesFolder"
- local StagesCompleted = Instance.new("IntValue",ValuesFolder)
- StagesCompleted.Name = "Stages Completed"
- local CoinsEarned = Instance.new("IntValue",ValuesFolder)
- CoinsEarned.Name = "Coins Earned"
- local TimePlayed = Instance.new("IntValue",ValuesFolder)
- TimePlayed.Name = "Time Played"
- local Deaths = Instance.new("IntValue",ValuesFolder)
- Deaths.Name = "Deaths"
- local userId = Player.UserId
- local key = "Player_"..userId
- local success, returnValue
- success, returnValue = pcall(MainStatsDatastore.GetAsync, MainStatsDatastore, key)
- if success then
- if returnValue == nil then
- returnValue = {
- Wins = 0,
- Coins = 0,
- Gems = 0,
- StagesCompleted = 0,
- CoinsEarned = 0,
- TimePlayed = 0,
- Deaths = 0,
- }
- end
- Wins.Value = if returnValue.Wins ~= nil then returnValue.Wins else 0
- Coins.Value = if returnValue.Coins ~= nil then returnValue.Coins else 0
- Gems.Value = if returnValue.Gems ~= nil then returnValue.Gems else 0
- StagesCompleted.Value = if returnValue.StagesCompleted ~= nil then returnValue.StagesCompleted else 0
- CoinsEarned.Value = if returnValue.CoinsEarned ~= nil then returnValue.CoinsEarned else 0
- TimePlayed.Value = if returnValue.TimePlayed ~= nil then returnValue.TimePlayed else 0
- Deaths.Value = if returnValue.Deaths ~= nil then returnValue.Deaths else 0
- else
- Player:Kick("There was an error with loading your Data! Roblox's Datastore might be down,try agen later or contact us trough our Group!")
- print(Player.Name.. " Has a Data loading ERROR!!")
- end
- end
- end)
- game.Players.PlayerRemoving:Connect(function(Player)
- local userId = Player.UserId
- local key = "Player_"..userId
- local wins = Player.leaderstats.Wins.Value
- local coins = Player.leaderstats.Coins.Value
- local gems = Player.leaderstats:FindFirstChild("Gems 💎").Value
- local stagescompleted = Player.ValuesFolder:FindFirstChild("Stages Completed").Value
- local coinsearned = Player.ValuesFolder:FindFirstChild("Coins Earned").Value
- local timePlayed = Player.ValuesFolder:FindFirstChild("Time Played").Value
- local deaths = Player.ValuesFolder.Deaths.Value
- local DataTable = {
- Wins = wins,
- Coins = coins,
- Gems = gems,
- StagesCompleted = stagescompleted,
- CoinsEarned = coinsearned,
- TimePlayed = timePlayed,
- Deaths = deaths,
- }
- print(DataTable)
- local success,returnValue
- success,returnValue = pcall(MainStatsDatastore.UpdateAsync, MainStatsDatastore, key, function()
- return DataTable
- end)
- if success then
- print("MainStats Saved!")
- else
- print("Data Saving ERROR!!")
- end
- end)
- function SetupPlayerData(Player)
- local leaderstats = Instance.new("Folder",Player)
- leaderstats.Name = "leaderstats"
- local Wins = Instance.new("IntValue",leaderstats)
- Wins.Name = "Wins"
- local Coins = Instance.new("IntValue",leaderstats)
- Coins.Name = "Coins"
- local Gems = Instance.new("IntValue",leaderstats)
- Gems.Name = "Gems 💎"
- local ValuesFolder = Instance.new("Folder",Player)
- ValuesFolder.Name = "ValuesFolder"
- local StagesCompleted = Instance.new("IntValue",ValuesFolder)
- StagesCompleted.Name = "Stages Completed"
- local CoinsEarned = Instance.new("IntValue",ValuesFolder)
- CoinsEarned.Name = "Coins Earned"
- local TimePlayed = Instance.new("IntValue",ValuesFolder)
- TimePlayed.Name = "Time Played"
- local Deaths = Instance.new("IntValue",ValuesFolder)
- Deaths.Name = "Deaths"
- local userId = Player.UserId
- local key = "Player_"..userId
- local success, returnValue
- repeat
- waitForRequestBudget(Enum.DataStoreRequestType.GetAsync)
- success, returnValue = pcall(MainStatsDatastore.GetAsync, MainStatsDatastore, key)
- until success or not Players:FindFirstChild(Player.Name)
- if success then
- if returnValue == nil then
- returnValue = {
- Wins = 0,
- Coins = 0,
- Gems = 0,
- StagesCompleted = 0,
- CoinsEarned = 0,
- TimePlayed = 0,
- Deaths = 0,
- }
- end
- Wins.Value = if returnValue.Wins ~= nil then returnValue.Wins else 0
- Coins.Value = if returnValue.Coins ~= nil then returnValue.Coins else 0
- Gems.Value = if returnValue.Gems ~= nil then returnValue.Gems else 0
- StagesCompleted.Value = if returnValue.StagesCompleted ~= nil then returnValue.StagesCompleted else 0
- CoinsEarned.Value = if returnValue.CoinsEarned ~= nil then returnValue.CoinsEarned else 0
- TimePlayed.Value = if returnValue.TimePlayed ~= nil then returnValue.TimePlayed else 0
- Deaths.Value = if returnValue.Deaths ~= nil then returnValue.Deaths else 0
- else
- Player:Kick("There was an error with loading your Data! Roblox's Datastore might be down,try agen later or contact us trough our Group!")
- print(Player.Name.. " Has a Data loading ERROR!!")
- end
- end
- function Save(Player)
- local userId = Player.UserId
- local key = "Player_"..userId
- local wins = Player.leaderstats.Wins.Value
- local coins = Player.leaderstats.Coins.Value
- local gems = Player.leaderstats:FindFirstChild("Gems 💎").Value
- local stagescompleted = Player.ValuesFolder:FindFirstChild("Stages Completed").Value
- local coinsearned = Player.ValuesFolder:FindFirstChild("Coins Earned").Value
- local timePlayed = Player.ValuesFolder:FindFirstChild("Time Played").Value
- local deaths = Player.ValuesFolder.Deaths.Value
- local DataTable = {
- Wins = wins,
- Coins = coins,
- Gems = gems,
- StagesCompleted = stagescompleted,
- CoinsEarned = coinsearned,
- TimePlayed = timePlayed,
- Deaths = deaths,
- }
- print(DataTable)
- local success,returnValue
- repeat
- waitForRequestBudget(Enum.DataStoreRequestType.UpdateAsync)
- success,returnValue = pcall(MainStatsDatastore.UpdateAsync, MainStatsDatastore, key, function()
- return DataTable
- end)
- until success
- if success then
- print("MainStats Saved!")
- else
- print("Data Saving ERROR!!")
- end
- end
- function OnShutdown()
- if RunService:IsStudio()then
- task.wait(2)
- else
- local finished = Instance.new("BindableEvent")
- local allPlayers = Players:GetPlayers()
- local leftPlayers = #allPlayers
- for _, player in ipairs(allPlayers) do
- coroutine.wrap(function()
- Save(player)
- leftPlayers -= 1
- if leftPlayers == 0 then
- finished:Fire()
- end
- end)()
- end
- finished.Event:Wait()
- end
- end
- for _, player in ipairs(game.Players:GetPlayers()) do
- coroutine.wrap(SetupPlayerData)(player)
- end
- game:BindToClose(OnShutdown)
Advertisement
Advertisement