Advertisement
HowToRoblox

CashServer

Mar 15th, 2023
1,334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.22 KB | None | 0 0
  1. local dss = game:GetService("DataStoreService")
  2. local ds = dss:GetDataStore("DATA")
  3.  
  4. local mps = game:GetService("MarketplaceService")
  5.  
  6. local devProducts = require(game:GetService("ReplicatedStorage"):WaitForChild("DeveloperProducts"))
  7.  
  8.  
  9. function saveData(plr:Player)
  10.    
  11.     if not plr:FindFirstChild("DATA FAILED TO LOAD") then
  12.        
  13.         local plrCash = plr.leaderstats.Cash.Value
  14.        
  15.         local success, err = nil, nil
  16.        
  17.         while true do
  18.            
  19.             success, err = pcall(function()
  20.                 ds:SetAsync(plr.UserId .. " - Cash", plrCash)
  21.             end)
  22.            
  23.             if not success then
  24.                 warn(err)
  25.                 task.wait(0.02)
  26.             else
  27.                 break
  28.             end
  29.         end
  30.     end
  31. end
  32.  
  33. function loadData(plr:Player)
  34.    
  35.     local dataFailedWarning = Instance.new("StringValue")
  36.     dataFailedWarning.Name = "DATA FAILED TO LOAD"
  37.  
  38.     local success, plrData = nil, nil
  39.    
  40.     while true do
  41.         success, plrData = pcall(function()
  42.             return ds:GetAsync(plr.UserId .. " - Cash")
  43.         end)
  44.         task.wait(0.02)
  45.         if not success then
  46.             dataFailedWarning.Parent = plr
  47.             warn(plrData)
  48.         else
  49.             break
  50.         end
  51.     end
  52.     dataFailedWarning:Destroy()
  53.    
  54.     return plrData
  55. end
  56.  
  57. function setupLeaderstats(plr:Player, savedCash:number)
  58.    
  59.     local leaderstats = Instance.new("Folder")
  60.     leaderstats.Name = "leaderstats"
  61.     leaderstats.Parent = plr
  62.    
  63.     local cash = Instance.new("IntValue")
  64.     cash.Name = "Cash"
  65.     cash.Value = savedCash or 0
  66.     cash.Parent = leaderstats
  67. end
  68.  
  69. function handlePurchase(purchaseInfo)
  70.    
  71.     local plr = game.Players:GetPlayerByUserId(purchaseInfo.PlayerId)
  72.    
  73.     if not plr or plr:FindFirstChild("DATA FAILED TO LOAD") then
  74.         return Enum.ProductPurchaseDecision.NotProcessedYet
  75.     end
  76.    
  77.     local productID = purchaseInfo.ProductId
  78.    
  79.     if devProducts[productID] then
  80.         plr.leaderstats.Cash.Value += devProducts[productID].Cash
  81.        
  82.         return Enum.ProductPurchaseDecision.PurchaseGranted
  83.        
  84.     else
  85.         return Enum.ProductPurchaseDecision.NotProcessedYet
  86.     end
  87. end
  88.  
  89.  
  90. game.Players.PlayerRemoving:Connect(saveData)
  91. game:BindToClose(function()
  92.     for _, plr in pairs(game.Players:GetPlayers()) do
  93.         saveData(plr)
  94.     end
  95. end)
  96.  
  97. game.Players.PlayerAdded:Connect(function(plr)
  98.    
  99.     local plrData = loadData(plr)
  100.     setupLeaderstats(plr, plrData)
  101. end)
  102.  
  103.  
  104.  
  105.  
  106.  
  107. mps.ProcessReceipt = handlePurchase
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement