LawMixer

CreditSystem

Aug 15th, 2021
1,237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.07 KB | None | 0 0
  1. local DataStoreService = game:GetService("DataStoreService")
  2. local CreditDatastore = DataStoreService:GetDataStore("DevelopmentCreditsTest")
  3. local configfile = require(script.configData)
  4. local dataConfig = configfile.dataConfig
  5. local Credit = { }
  6. local QueuedPlayers = { }
  7. local RunService = game:GetService("RunService")
  8.  
  9. Credit.SetupPlayer = function(Player)
  10.     local Data = Instance.new("Folder")
  11.     Data.Name = "Data"
  12.     print(Player)
  13.     Data.Parent = Player.UserId
  14.    
  15.     local Successful, Save = Credit:LocateSave(Player)
  16.     local Credits = Instance.new("IntValue")
  17.     Credits.Name = "Credits"
  18.     Credits.Parent = Data
  19.    
  20.     Credits.Value = Save or dataConfig.StarterCash
  21.    
  22.     while Player do
  23.         wait(dataConfig.CreditCooldown)
  24.        
  25.         if Player ~= nil and Player:IsDescendantOf(game.Players) then
  26.        
  27.             Credits.Value += dataConfig.IncomeIncrease
  28.            
  29.         end
  30.     end
  31. end
  32.  
  33. function Credit:LocateSave(Player)
  34.    
  35.     local UserId = Player.UserId
  36.     print(UserId)
  37.     assert(type(UserId) == "number", "Credit:LocateSave() failed! (Invalid UserId)")
  38.    
  39.     local DataReturn = CreditDatastore:GetAsync(UserId)
  40.     return DataReturn ~= nil, DataReturn
  41. end
  42.  
  43. function Credit:SavePlayer(Player)
  44.    
  45.     if RunService:IsStudio() then
  46.         return
  47.     end
  48.    
  49.     local UserId = Player.UserId
  50.     print(UserId)
  51.     assert(type(UserId) == "number", "Credit:LocateSave() failed! (Invalid UserId)")
  52.    
  53.     local Data = Player:FindFirstChild("Data"):FindFirstChild("Credits")
  54.    
  55.     if not Data then
  56.         Data = { }
  57.         Data["Value"] = dataConfig.StarterCash
  58.     end
  59.    
  60.     local Successful = pcall(function()
  61.         CreditDatastore:SetAsync(UserId, Data.Value)
  62.     end)
  63.    
  64.     if not Successful then
  65.         warn("[DATASTORE] UNSUCCESSFUL SAVE OF ".. Player.Name.. "! RETRYING")
  66.         delay(dataConfig.RetryTime, function()
  67.             self:SavePlayer(Player)
  68.         end)
  69.     else
  70.         print("[DATASTORE] SUCCESSFUL SAVE OF ".. Player.Name)
  71.     end
  72.    
  73. end
  74.  
  75. Credit.SaveAllPlayers = function()
  76.     for _, Player in next, game.Players:GetPlayers() do
  77.         local Credits = Player:FindFirstChild("Data"):FindFirstChild("Credits").Value
  78.         Credit:SavePlayer(Player)
  79.     end
  80. end
  81.  
  82.  
  83. return Credit
Advertisement
Add Comment
Please, Sign In to add comment