Advertisement
Guest User

Stats

a guest
Jun 24th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.76 KB | None | 0 0
  1. local dataStores = game:GetService("DataStoreService"):GetDataStore("BucksDataStore")
  2. local dataStoresB = game:GetService("DataStoreService"):GetDataStore("WinsDataStore")
  3. local DefaultCash = 1000
  4. local DefaultWins = 0
  5. local playersLeft = 0
  6.  
  7. game.Players.PlayerAdded:Connect(function(player)
  8.    
  9.         playersLeft = playersLeft + 1
  10.    
  11.         local leaderstats = Instance.new("Folder")
  12.         leaderstats.Name = "leaderstats"
  13.         leaderstats.Parent = player
  14.        
  15.         local bucks = Instance.new("IntValue")
  16.         bucks.Name = "Bucks"
  17.         bucks.Value = 0
  18.         bucks.Parent = leaderstats
  19.        
  20.         local wins = Instance.new("IntValue")
  21.         wins.Name = "Wins"
  22.         wins.Value = 0
  23.         wins.Parent = leaderstats
  24.        
  25.         local playerData = Instance.new("Folder")
  26.         playerData.Name = player.Name
  27.         playerData.Parent = game.ServerStorage.PlayerData
  28.        
  29.         local equipped = Instance.new("StringValue")
  30.         equipped.Name = "Equipped"
  31.         equipped.Parent = playerData
  32.        
  33.         local inventory = Instance.new("Folder")
  34.         inventory.Name = "Inventory"
  35.         inventory.Parent = playerData
  36.        
  37.         player.CharacterAdded:Connect(function(character)
  38.             character.Humanoid.Died:Connect(function()
  39.                 --Whenever somebody gets pwned, this event will run
  40.                 if character:FindFirstChild("GameTag") then
  41.                     character.GameTag:Destroy()
  42.                 end
  43.                
  44.                 player:LoadCharacter()
  45.             end)
  46.            
  47.         end)
  48.        
  49.         --Date Stores
  50.        
  51.         local player_data
  52.         local weaponsData
  53.         local equippedData
  54.         local player_dataB
  55.        
  56.         pcall(function()
  57.             player_data = dataStores:GetAsync(player.UserId.."-Bucks") --1736284-Bucks
  58.         end)
  59.        
  60.         pcall(function()
  61.             weaponsData = dataStores:GetAsync(player.UserId.."-Weps") --1736284-Bucks
  62.         end)
  63.        
  64.         pcall(function()
  65.              equippedData = dataStores:GetAsync(player.UserId.."-EquippedValue") --1736284-Bucks
  66.         end)
  67.        
  68.         pcall(function()
  69.             player_dataB = dataStores:GetAsync(player.UserId.."-Wins")
  70.         end)
  71.        
  72.         if player_data ~= nil then
  73.             --Player has saved data, load this in
  74.             bucks.Value = player_data
  75.         else
  76.             -- New Player
  77.             bucks.Value = DefaultCash
  78.         end
  79.        
  80.         if player_dataB ~= nil then
  81.             --Player has saved data, load this in
  82.             wins.Value = player_dataB
  83.         else
  84.             -- New Player
  85.             wins.Value = DefaultWins
  86.         end
  87.        
  88.         if weaponsData then
  89.            
  90.             for _, weapon in pairs(weaponsData) do
  91.                 if game.ServerStorage.Items:FindFirstChild(weapon) then
  92.                     local weaponClone = game.ServerStorage.Items[weapon]:Clone()
  93.                     weaponClone.Parent = inventory
  94.                     print(weapon.." loaded in!")
  95.                 end
  96.             end
  97.            
  98.             if equippedData then
  99.                 equipped.Value = equippedData
  100.                 player:WaitForChild("PlayerGui")
  101.                 game.ReplicatedStorage.SendEquipped:FireClient(player,equippedData)
  102.             end
  103.            
  104.         else
  105.             print("No weapons data")
  106.         end
  107.        
  108. end)
  109.  
  110. local bindableEvent = Instance.new("BindableEvent")
  111.  
  112. game.Players.PlayerRemoving:Connect(function(player)
  113.    
  114.     pcall(function()
  115.         dataStores:SetAsync(player.UserId.."-Bucks",player.leaderstats.Bucks.Value)
  116.         dataStores:SetAsync(player.UserId.."-Wins",player.leaderstats.Wins.Value)
  117.         playersLeft = playersLeft - 1
  118.         bindableEvent:Fire()
  119.     end)
  120.    
  121.     pcall(function()
  122.         local weapons = game.ServerStorage.PlayerData[player.Name].Inventory:GetChildren()
  123.         local weaponsTable = {}
  124.        
  125.         for _, v in pairs(weapons) do
  126.             table.insert(weaponsTable,v.Name)
  127.         end
  128.        
  129.     dataStores:SetAsync(player.UserId.."-Weps",weaponsTable)
  130.    
  131.     if game.ServerStorage.PlayerData[player.Name].Equipped.Value ~= nil then
  132.         local equippedVal = game.ServerStorage.PlayerData[player.Name].Equipped
  133.         dataStores:SetAsync(player.UserId.."-EquippedValue",equippedVal.Value)
  134.     end
  135.     end)
  136.    
  137.     print("Saved weapons, wins, and points.")
  138.    
  139. end)
  140.  
  141. game:BindToClose(function()
  142.     -- This will be triggered upon shutdown
  143.     while playersLeft > 0 do
  144.         bindableEvent.Event:Wait()
  145.     end
  146. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement