Advertisement
Sungmingamerpro13

CashDataStore

Mar 10th, 2025
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
CSS 1.00 KB | None | 0 0
  1. local CashDataStore = game:GetService("DataStoreService"):GetDataStore("Cash")
  2.  
  3. game.Players.PlayerAdded:Connect(function(player)
  4.     local previousData = CashDataStore:GetAsync(player.UserId)
  5.     local Cash
  6.    
  7.     if previousData ~= nil then
  8.         Cash = previousData
  9.     else
  10.         Cash = 0
  11.         CashDataStore:SetAsync(player.UserId, Cash)
  12.     end
  13.    
  14.     local CashValue = Instance.new("NumberValue", player)
  15.     CashValue.Name = "Cash"
  16.     CashValue.Value = Cash
  17.    
  18. end)
  19.  
  20. game:BindToClose(function()
  21.     for i,player in pairs(game.Players:GetPlayers()) do
  22.         local value = player.Cash.Value
  23.         CashDataStore:SetAsync(player.UserId, value)
  24.         print("Saved data for "..player.Name)
  25.     end
  26. end)
  27.  
  28. game.Players.PlayerRemoving:Connect(function(player)
  29.     local value = player.Cash.Value
  30.  
  31.     if value ~= nil then
  32.         print("Found data to save for "..player.Name.."!")
  33.         CashDataStore:SetAsync(player.UserId, value)
  34.         print("Saved data for "..player.Name)
  35.     else
  36.         print("Did not manage to find data to save for "..player.Name.."!")
  37.     end
  38. end)
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement