Advertisement
HowToRoblox

LevelServer

Sep 11th, 2021
4,487
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.47 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local levelDS = dss:GetDataStore("Levels")
  3.  
  4.  
  5. function incrementExp(player, increment)
  6.    
  7.     for i = player.Stats.Experience.Value, player.Stats.Experience.Value + increment do
  8.        
  9.         player.Stats.Experience.Value = i
  10.        
  11.         wait()
  12.     end
  13. end
  14.  
  15.  
  16. function saveData(player)
  17.    
  18.     pcall(function()
  19.        
  20.         local level = player.Stats.Level.Value
  21.         local exp = player.Stats.Experience.Value
  22.        
  23.         levelDS:SetAsync(player.UserId .. "Level", {level, exp})
  24.     end)
  25. end
  26.  
  27.  
  28. game.Players.PlayerAdded:Connect(function(player)
  29.    
  30.    
  31.     local statsFolder = Instance.new("Folder", player)
  32.     statsFolder.Name = "Stats"
  33.    
  34.     local levelVal = Instance.new("IntValue", statsFolder)
  35.     levelVal.Name = "Level"
  36.     levelVal.Value = 1
  37.    
  38.     local expVal = Instance.new("IntValue", statsFolder)
  39.     expVal.Name = "Experience"
  40.    
  41.    
  42.     pcall(function()
  43.        
  44.         local data = levelDS:GetAsync(player.UserId .. "Level")
  45.        
  46.         if data then
  47.            
  48.             levelVal.Value = data[1]
  49.             expVal.Value = data[2]
  50.         end
  51.     end)
  52.    
  53.    
  54.     expVal:GetPropertyChangedSignal("Value"):Connect(function()
  55.        
  56.         local neededExp = math.floor(levelVal.Value ^ 1.5 + 0.5) * 500
  57.        
  58.         if expVal.Value >= neededExp then
  59.            
  60.             levelVal.Value += 1
  61.         end
  62.     end)
  63.    
  64.    
  65.     while wait(0.2) do
  66.        
  67.         incrementExp(player, 100)
  68.     end
  69. end)
  70.  
  71.  
  72. game.Players.PlayerRemoving:Connect(saveData)
  73.  
  74. game:BindToClose(function()
  75.    
  76.     for i, player in pairs(game.Players:GetPlayers()) do
  77.        
  78.         saveData(player)
  79.     end
  80. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement