Advertisement
facedwarrior193

Leaderstats

Mar 17th, 2022
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.60 KB | None | 0 0
  1. local RunService = game:GetService("RunService")
  2. local DataStoreService = game:GetService("DataStoreService")
  3. local dataStore = DataStoreService:GetDataStore("MyDataStore")
  4. local function saveData(player) -- The functions that saves data
  5.  
  6.     local tableToSave = {
  7.         player.leaderstats.Wins.Value;
  8.         player.leaderstats.Kills.Value;
  9.         player.leaderstats.Deaths.Value
  10.  
  11.     }
  12.  
  13.     local success, err = pcall(function()
  14.         dataStore:SetAsync(player.UserId, tableToSave)
  15.     end)
  16.  
  17.     if success then
  18.         print("Data has been saved!")
  19.     else -- Else if the save failed
  20.         print("Data hasn't been saved!")
  21.         warn(err)
  22.     end
  23. end
  24.  
  25. game.Players.PlayerAdded:Connect(function(player)
  26.  
  27.     -- // Assigning player stats //
  28.     local leaderstats = Instance.new("Folder")
  29.     leaderstats.Name = "leaderstats"
  30.     leaderstats.Parent = player
  31.  
  32.     local Wins = Instance.new("IntValue")
  33.     Wins.Name = "Wins"
  34.     Wins.Parent = leaderstats
  35.  
  36.     local Kills = Instance.new("IntValue")
  37.     Kills.Name = "Kills"
  38.     Kills.Parent = leaderstats
  39.  
  40.     local Deaths = Instance.new("IntValue")
  41.     Deaths.Name = "Deaths"
  42.     Deaths.Parent = leaderstats
  43.  
  44.     local data
  45.     local success, err = pcall(function()
  46.  
  47.         data = dataStore:GetAsync(player.UserId)
  48.  
  49.     end)
  50.  
  51.     if success and data then
  52.  
  53.         Wins.Value = data[1]
  54.         Kills.Value = data[2]
  55.         Deaths.Value = data[3]
  56.     else
  57.         print("The player has no data!")
  58.     end
  59.  
  60. end)
  61.  
  62. game.Players.PlayerRemoving:Connect(saveData)
  63.  
  64. game:BindToClose(function()
  65.     if RunService:IsStudio() then
  66.         task.wait(2)
  67.     else
  68.         for _, player in ipairs(game.Players:GetPlayers()) do
  69.             coroutine.wrap(saveData)(player)
  70.         end
  71.     end
  72. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement