Advertisement
HowToRoblox

ATMServer

Apr 13th, 2022
2,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.99 KB | None | 0 0
  1. local re = game.ReplicatedStorage:WaitForChild("ATMRemoteEvent")
  2.  
  3. local dss = game:GetService("DataStoreService")
  4. local ds = dss:GetDataStore("ATM")
  5.  
  6. local pps = game:GetService("ProximityPromptService")
  7.  
  8.  
  9. function saveData(plr)
  10.  
  11.     local cash = plr.leaderstats.Cash.Value
  12.  
  13.     pcall(function()
  14.         ds:SetAsync(plr.UserId .. "Cash", cash)
  15.     end)
  16. end
  17.  
  18.  
  19. game.Players.PlayerAdded:Connect(function(plr)
  20.  
  21.     local ls = Instance.new("Folder", plr)
  22.     ls.Name = "leaderstats"
  23.  
  24.     local cash = Instance.new("IntValue")
  25.     cash.Name = "Cash"
  26.     cash.Parent = ls
  27.  
  28.     local plrCash
  29.     pcall(function()
  30.         plrCash = ds:GetAsync(plr.UserId .. "Cash")
  31.     end)
  32.    
  33.     local depositedCash
  34.     pcall(function()
  35.         depositedCash = ds:GetAsync(plr.UserId .. "Deposited")
  36.     end)
  37.    
  38.     if not depositedCash then
  39.        
  40.         ds:SetAsync(plr.UserId .. "Deposited", 0)
  41.     end
  42.  
  43.     cash.Value = plrCash or 0
  44. end)
  45.  
  46.  
  47. game.Players.PlayerRemoving:Connect(saveData)
  48.  
  49. game:BindToClose(function()
  50.  
  51.     for i, plr in pairs(game.Players:GetPlayers()) do
  52.  
  53.         saveData(plr)
  54.     end
  55. end)
  56.  
  57.  
  58. pps.PromptTriggered:Connect(function(prompt, plr)
  59.  
  60.     if prompt.Name == "ATMProximityPrompt" then
  61.  
  62.         local cashStored
  63.  
  64.         pcall(function()
  65.             cashStored = ds:GetAsync(plr.UserId .. "Deposited")
  66.         end)
  67.  
  68.         if cashStored then
  69.  
  70.             re:FireClient(plr, cashStored)
  71.         end
  72.     end
  73. end)
  74.  
  75.  
  76. re.OnServerEvent:Connect(function(plr, amount, option)
  77.  
  78.     if amount and option and tonumber(amount) then
  79.  
  80.         local cashStored
  81.  
  82.         pcall(function()
  83.             cashStored = ds:GetAsync(plr.UserId .. "Deposited")
  84.         end)
  85.  
  86.         if not cashStored then return end
  87.  
  88.         if option == "deposit" then
  89.  
  90.             if tonumber(amount) <= plr.leaderstats.Cash.Value then
  91.  
  92.                 plr.leaderstats.Cash.Value -= amount
  93.  
  94.                 cashStored += amount
  95.             end
  96.  
  97.         elseif option == "withdraw" then
  98.  
  99.             if tonumber(amount) <= cashStored then
  100.  
  101.                 plr.leaderstats.Cash.Value += amount
  102.  
  103.                 cashStored -= amount
  104.             end
  105.         end
  106.  
  107.         pcall(function()
  108.             ds:SetAsync(plr.UserId .. "Deposited", cashStored)
  109.         end)
  110.     end
  111. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement