Advertisement
EconomicSerg

Roblox DataStore

Feb 7th, 2021
316
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.04 KB | None | 0 0
  1. local DataStoreService = game:GetService("DataStoreService")
  2. local myDataStore = DataStoreService:GetDataStore("MyDataStore")
  3.  
  4. game.Players.PlayerAdded:Connect(function(player)
  5.    
  6.     local leaderstats = Instance.new("Folder", player)
  7.     leaderstats.Name = "leaderstats"
  8.    
  9.     local cash = Instance.new("IntValue", leaderstats)
  10.     cash.Name = "Cash"
  11.    
  12.    
  13.     local playerUserId = "Player_"..player.UserId
  14.    
  15.     -- Load data
  16.    
  17.     local data
  18.     local success, errormessage = pcall(function()
  19.         data = myDataStore:GetAsync(playerUserId)
  20.     end)
  21.    
  22.     if success then
  23.         cash.Value = data
  24.         -- Set our data equal to the current cash
  25.     end
  26. end)
  27.  
  28. game.Players.PlayerRemoving:Connect(function(player)
  29.     local playerUserId = "Player_"..player.UserId
  30.    
  31.     local data = player.leaderstats.Cash.Value
  32.    
  33.     local success, errormessage = pcall(function()
  34.         myDataStore:SetAsync(playerUserId, data)
  35.     end)
  36.    
  37.     if success then
  38.         print("Successfully saved!")
  39.     else
  40.         print("There was an error!")
  41.         warn(errormessage)
  42.     end
  43.    
  44.     myDataStore:SetAsync(playerUserId, data)
  45. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement