Richbadniss

DataStore system V2

Oct 20th, 2021 (edited)
278
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.33 KB | None | 0 0
  1. --> Services
  2. local Workspace                 =game:GetService('Workspace')
  3. local RunService                =game:GetService('RunService')
  4. local MarketPlaceService        =game:GetService('MarketplaceService')
  5. local DataStoreService          =game:GetService('DataStoreService')
  6. local Players                   =game:GetService('Players')
  7. local ReplicatedStorage         =game:GetService('ReplicatedStorage')
  8. local ReplicatedFirst           =game:GetService('ReplicatedFirst')
  9. local ServerScriptService       =game:GetService('ServerScriptService')
  10. local ServerStorage             =game:GetService('ServerStorage')
  11. local StarterPack               =game:GetService('StarterPack')
  12. local Teams                     =game:GetService('Teams')
  13. local Chat                      =game:GetService('Chat')
  14. local StarterGui                =game:GetService('StarterGui')
  15.  
  16. local DataStore = DataStoreService:GetDataStore('DataStore1')
  17.  
  18. local Tries = 3 --> Kicks from game if cant load data within 3 tries (I don't know how to explain that anybetter)
  19. local DataLoaded = false
  20.  
  21.  
  22. --> Setting/ Saving the Players Data
  23. local function setData(Player)
  24.     print(Player)
  25.     --> This will only run if the player(s) data has loaded
  26.     if DataLoaded then
  27.         --> Variables
  28.         local DataKey  = Player.UserId
  29.         local Count = 0
  30.        
  31.         local Data = {
  32.             ['Fossils'] = Player.LeaderBoard.Fossils.Value,
  33.             ['Coins'] = Player.LeaderBoard.Coins.Value,
  34.             ['Gems'] = Player.LeaderBoard.Gems.Value
  35.         }
  36.        
  37.         local Succsess, Error
  38.         --> This loops to see if the players data has saved(Handles Error)
  39.        
  40.         repeat
  41.             Succsess, Error = pcall(function()
  42.                 DataStore:SetAsync(DataKey, Data)
  43.             end)
  44.             Count = Count +1
  45.         until Count >= Tries or Succsess
  46.        
  47.        
  48.         --> If data didn't save prints/ warns and error(Handles error)
  49.         if not Succsess then
  50.             warn('Data Failded to save, Code: '.. tostring(Error))
  51.            
  52.             return
  53.         end
  54.     else
  55.         return
  56.     end
  57. end
  58.  
  59. -->Loading/ Getting the players Data
  60. local function GetData(Player)
  61. print(Player:GetFullName())
  62.     --> Variables
  63.     local DataKey = Player.UserId
  64.     local Count = 0 --> It hasnt began trying yet
  65.    
  66.     local Data  --> An return Value for the pcall(function())
  67.     local Success, Error
  68.    
  69.     repeat
  70.         Success, Error = pcall(function()
  71.             Data = DataStore:GetAsync(DataKey)
  72.         end)
  73.        
  74.         Count = Count +1 --> We will loop until their data loads (This is were the amount of tries comes in to play)
  75.     until Count >= Tries or Success
  76.    
  77.     --> If something went wrong while loading data the player get kick
  78.    
  79.     if not Success then
  80.         wait()
  81.         warn('Data failded, Error: '..tostring(Error))
  82.        
  83.         Player:Kick('Your Data has faild to load. Please rejoin to try again (Automated Kick)') --> Kicking the player because their data isnt loading or hasnt loaded within the required time(3)
  84.         return         
  85.     end
  86.    
  87.     --> This looks for a new player And gives them new data
  88.     if Success then
  89.         if Data then
  90.             return Data --> If a player has already played the game it will return there original data
  91.            
  92.         else
  93.             return
  94.                 {
  95.                 ['Fossils'] = 0,
  96.                 ['Coins']  = 0, --> Default (FOR NEW PLAYERS)
  97.                 ['Gems'] = 0    --> Default (FOR NEW PLAYERS)
  98.             }
  99.         end
  100.        
  101.     end
  102. end
  103.  
  104. -->Leaderboard
  105. local function CreatPlayerLeaderBoard(Player)
  106.     print(Player)
  107.     local Values = GetData(Player)
  108.     local leaderboard = Instance.new('Folder')
  109.     leaderboard.Name = 'LeaderBoard' --> Giving the LeaderBoard a name (Mainstats) beacuse i dont want it to shoe up
  110.     leaderboard.Parent = Player --> Setting It's parent to the player
  111.    
  112.     local Fossil = Instance.new('NumberValue') --> Data in the players Inventory/ Wallet
  113.     Fossil.Name = 'Fossils'
  114.     Fossil.Parent = leaderboard
  115.    
  116.     local Coins = Instance.new('NumberValue') --> Data in the players Bank
  117.     Coins.Name = 'Coins'
  118.     Coins.Parent = leaderboard
  119.    
  120.     local Gems = Instance.new('NumberValue') --> Data in the players Inventory/ Wallet
  121.     Gems.Name = 'Gems'
  122.     Gems.Parent = leaderboard
  123.    
  124.  
  125.      
  126.     --> Setting Player Data Values
  127.     Coins.Value = Values.Coins
  128.     Gems.Value = Values.Gems
  129.     Fossil.Value = Values.Fossils
  130.     DataLoaded = true
  131.    
  132.     local function SaveDataOnChanged(DataValue)
  133.          DataValue.Changed:Connect(function()
  134.             print('Changed...')
  135.             setData(Player)
  136.         end)
  137.     end
  138.     SaveDataOnChanged(Player.LeaderBoard.Fossils)
  139. end
  140.  
  141.  
  142.  
  143.  
  144. Players.PlayerRemoving:Connect(setData)
  145. Players.PlayerAdded:Connect(CreatPlayerLeaderBoard)
  146.  
  147. game:BindToClose(function()
  148.     for i, User in next, game.Players:GetChildren() do
  149.         if User then
  150.             setData(User)
  151.         end
  152.     end
  153. end)
  154.  
Add Comment
Please, Sign In to add comment