LawMixer

ModuleScript (Credit Functions)

Oct 9th, 2021
1,116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.40 KB | None | 0 0
  1. -- Start of Credit Functions
  2. local DataStoreService = game:GetService("DataStoreService")
  3. local CreditDatastore = DataStoreService:GetDataStore("CreditSystemDev")
  4.  
  5. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  6. local Players = game:GetService("Players")
  7.  
  8. local FirebaseAPI = require(script.FirebaseService)
  9. local Database = FirebaseAPI:GetFirebase("Nitro")
  10.  
  11. local configfile = require(script.configData)
  12. local dataConfig = configfile.dataConfig
  13. local Credit = { }
  14. local RunService = game:GetService("RunService")
  15.  
  16.  
  17. -- sets the player up & if they already have a save, load that save in and not give them the default starter cash
  18. Credit.SetupPlayer = function(Player)
  19.     for _, plr in next, game.Players:GetPlayers() do
  20.         local Data = Instance.new("Folder")
  21.         Data.Name = "Data"
  22.         Data.Parent = plr
  23.  
  24.         local Successful, Save = Credit:LocateSave(Player)
  25.         local Credits = Instance.new("IntValue")
  26.         Credits.Name = "Credits"
  27.         Credits.Parent = Data
  28.  
  29.         Credits.Value = Save or dataConfig.StarterCash
  30.     end
  31. end
  32.  
  33. -- locates the save of the player
  34. function Credit:LocateSave(Player)
  35.     for _, plr in next, game.Players:GetPlayers() do
  36.         local UserId = plr.UserId
  37.         local DataReturn = CreditDatastore:GetAsync(UserId)
  38.         return DataReturn ~= nil, DataReturn
  39.     end
  40. end
  41.  
  42. -- saves the player data
  43. function Credit:SavePlayer(Player)
  44.  
  45.     if RunService:IsStudio() then
  46.         return
  47.     end
  48.  
  49.  
  50.     local UserId = Player.UserId
  51.     assert(type(UserId) == "number", "Credit:LocateSave() failed! (Invalid UserId)")
  52.  
  53.     local Data = Player:WaitForChild("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.         delay(dataConfig.RetryTime, function()
  66.             Player:SavePlayer(Player)
  67.         end)
  68.         error("[DATASTORE] UNSUCCESSFUL SAVE OF ".. Player.Name.. "! RETRYING")
  69.     else
  70.         print("[DATASTORE] SUCCESSFUL SAVE OF ".. Player.Name)
  71.     end
  72.  
  73. end
  74.  
  75. -- goes through and saves all players data
  76. Credit.SaveAllPlayers = function()
  77.     if RunService:IsStudio() then
  78.         print("You're in studio, you cannot test if this works.")
  79.         return
  80.     end
  81.  
  82.     for _, Player in next, game.Players:GetPlayers() do
  83.         local Credits = Player:FindFirstChild("Data"):FindFirstChild("Credits").Value
  84.         Credit:SavePlayer(Player)
  85.     end
  86. end
  87.  
  88. -- updates the player's text on gui
  89. function Credit.Update(player)
  90.     for _, Player in next, game.Players:GetPlayers() do
  91.         local Credits = Player:WaitForChild("Data"):FindFirstChild("Credits").Value
  92.         Player:WaitForChild("PlayerGui"):WaitForChild("Credits").CreditTex.TextLabel.Text = "Credits: " .. Credits
  93.     end
  94. end
  95.  
  96. -- gives nitro boost perks whenever boosted our server
  97. function Credit.Nitro(player)
  98.     print("starting up nitro")
  99.     -- gives the players that nitro boosted our server, some perks, enjoy :D
  100.  
  101.     -- setting up firebase
  102.     local FirebaseAPI = require(script.FirebaseService)
  103.     local NiDatabase = FirebaseAPI:GetFirebase("Nitro")
  104.     local PlayerData = NiDatabase:GetAsync(player.UserId.."/nitro")
  105.  
  106.  
  107.     -- giving the players perks if the value retuns true
  108.     if PlayerData then
  109.         local Credit = player:WaitForChild("Data"):WaitForChild("Credits").Value
  110.         local ReplicatedStorage = game:GetService("ReplicatedStorage")
  111.  
  112.         Credit += 10000
  113.     end
  114.     print("completed nitro")
  115. end
  116.  
  117. return Credit
  118. -- ending of the credit function script
Advertisement
Add Comment
Please, Sign In to add comment