LawMixer

sss

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