Advertisement
Cakey3101

DataStore Script!

Mar 16th, 2024
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.23 KB | Source Code | 0 0
  1. local DataStoreService = game:GetService("DataStoreService") -- Getting The Main Service
  2. local Players = game:GetService("Players") -- Getting This Service For Connecting Events For A Player
  3.  
  4. local DataStore = DataStoreService:GetDataStore("1") -- If You Change This 1 To Another Number, All Data Will Be Reset
  5.  
  6. Players.PlayerAdded:Connect(function(player: Player) -- Using The PlayerAdded Event
  7.     local data
  8.     local success, errorMsg = pcall(function()
  9.         DataStore:GetAsync(player.UserId.."-points") -- Retrieving The Player's Data With A Specific Key
  10.     end)
  11.    
  12.     if success then
  13.         data = player.leaderstats.Points.Value -- Setting Data To The Player's Points
  14.     else
  15.         print("There Was An Error Loading "..player.Name.." 'S Data!")
  16.         wait(errorMsg)
  17.     end
  18. end)
  19.  
  20. Players.PlayerRemoving:Connect(function(player: Player) -- Using The PlayerRemoving Event
  21.     local success, errorMsg = pcall(function()
  22.         DataStore:SetAsync(player.UserId.."-points", player.leaderstats.Points.Value) -- Setting A Specific Key For This Stat
  23.     end)
  24.    
  25.     if success == true then
  26.         print(player.Name.." 'S Data Was Saved!") -- Printing If A Player's Data Was Saved Correctly
  27.     else
  28.         print("There Was An Error Saving "..player.Name.."'S Data!")
  29.         warn(errorMsg)
  30.     end
  31. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement