Advertisement
GaryScripts

DataStore Roblox

Jan 30th, 2021
2,618
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.93 KB | None | 0 0
  1. local DDS = game:GetService("DataStoreService")
  2. local DataStore = DDS:GetDataStore("MyData")
  3.  
  4. game.Players.PlayerAdded:Connect(function(p)
  5.     local leaderstats = Instance.new("Folder", p)
  6.     leaderstats.Name = "leaderstats"
  7.    
  8.     local Hammers = Instance.new("IntValue", leaderstats)
  9.     Hammers.Name = "Hammers"
  10.    
  11.     local data
  12.     local success, err = pcall(function()
  13.         data = DataStore:GetAsync(p.userId.."-Hammers")
  14.     end)
  15.    
  16.     if success then
  17.         print("data successfully retrieved")
  18.         Hammers.Value = data
  19.     elseif err then
  20.         warn("data didn't get retrieved")
  21.         p:Kick("Your data didn't get retrieved correctly. Please rejoin, or contact a database admin.")
  22.     end
  23. end)
  24.  
  25. game.Players.PlayerRemoving:Connect(function(p)
  26.     local success, err = pcall(function()
  27.         DataStore:SetAsync(p.userId.."-Hammers", p.leaderstats.Hammers.Value)
  28.     end)
  29.    
  30.     if success then
  31.         print("saved data")
  32.     elseif err then
  33.         warn("data unsuccessfully saved")
  34.     end
  35. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement