Sungmingamerpro13

LivesDataStore for Story Games

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