Advertisement
sriyanto

Stats

Oct 2nd, 2022
1,092
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.19 KB | None | 0 0
  1. local dataStores = game:GetService("DataStoreService"):GetDataStore("BucksDataStore")
  2.  
  3. local defaultCash = 10
  4.  
  5. local playersLeft = 0
  6.  
  7.  
  8.  
  9. game.Players.PlayerAdded:Connect(function(player)
  10.  
  11.    
  12.  
  13.     playersLeft = playersLeft + 1
  14.  
  15.    
  16.  
  17.     --to create a leaderboard
  18.  
  19.     local leaderstats = Instance.new("Folder")
  20.  
  21.     leaderstats.Name = "leaderstats"--leaderstats should be mentioned like this only, otherwise the leaderboard will not pop up
  22.  
  23.    
  24.  
  25.     --parent the leaderstats folder so that every player will have their own folder called leaderstats
  26.  
  27.     leaderstats.Parent = player
  28.  
  29.    
  30.  
  31.     --create a variable for the currency
  32.  
  33.     local bucks = Instance.new("IntValue") --IntValue means integer value since currency will be in the form of numbers
  34.  
  35.     bucks.Name = "Bucks"
  36.  
  37.     bucks.Value = 0                             --Initally the value will be 0
  38.  
  39.     bucks.Parent = leaderstats
  40.  
  41.    
  42.  
  43.     player.CharacterAdded:Connect(function(character)
  44.  
  45.         character.Humanoid.WalkSpeed = 16
  46.  
  47.         character.Humanoid.Died:Connect(function()
  48.  
  49.             --whenever somebody dies, this event will run
  50.  
  51.            
  52.  
  53.             if character.Humanoid and character.Humanoid:FindFirstChild("creator") then
  54.  
  55.                 game.ReplicatedStorage.Status.Value = tostring(character.Humanoid.creator.Value).." KILLED "..player.Name
  56.  
  57.             end
  58.  
  59.            
  60.  
  61.            
  62.  
  63.             if character:FindFirstChild("GameTag") then
  64.  
  65.                 character.GameTag:Destroy()
  66.  
  67.             end
  68.  
  69.             player:LoadCharacter()
  70.  
  71.         end)
  72.  
  73.     end)
  74.  
  75.    
  76.  
  77.     --Data Stores
  78.  
  79.    
  80.  
  81.     local player_data
  82.  
  83.    
  84.  
  85.     pcall (function()
  86.  
  87.         player_data = dataStores:GetAsync(player.UserId.." -Bucks")
  88.  
  89.     end)
  90.  
  91.    
  92.  
  93.     if player_data ~= nil then
  94.  
  95.         --player has saved data, load it in
  96.  
  97.         bucks.Value = player_data
  98.  
  99.     else
  100.  
  101.         --New Player
  102.  
  103.         bucks.Value = defaultCash
  104.  
  105.     end
  106.  
  107.    
  108.  
  109. end)
  110.  
  111.  
  112.  
  113. local bindableEvent = Instance.new("BindableEvent")
  114.  
  115.  
  116.  
  117. game.Players.PlayerRemoving:Connect(function(player)
  118.  
  119.     pcall(function()
  120.  
  121.         dataStores:SetAsync(player.UserId.."-Bucks",player.leaderstats.Bucks.Value)
  122.  
  123.         print("Saved")
  124.  
  125.         playersLeft = playersLeft-1
  126.  
  127.         bindableEvent:Fire()
  128.  
  129.     end)
  130.  
  131. end)
  132.  
  133.  
  134.  
  135.  
  136.  
  137. game:BindToClose(function()
  138.  
  139.     --This will be triggered upon shutdown
  140.  
  141.     while playersLeft > 0 do
  142.  
  143.         bindableEvent.Event:Wait()
  144.  
  145.     end
  146.  
  147. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement