Advertisement
SoloVisionGaming

Auto save leaderbored

Mar 9th, 2021
341
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ds = game:GetService("DataStoreService"):GetDataStore("Points") -- GetDataStore provides a key to save everything under.  Can be anything.
  2.  
  3. game.Players.PlayerAdded:connect(function(player)
  4.     local leaderstats = Instance.new("IntValue",player) -- Creating the leaderboard
  5.     leaderstats.Name = "leaderstats"
  6.     local points = Instance.new("IntValue",player)
  7.     points.Name = "Points"
  8.     points.Value = ds:GetAsync(player.Name) or 0 -- Here we look for the players name under "Points".  DataStores act like a table, and if the players name doesn't yet exist, it sets the points to 0 by default.
  9. end)
  10.  
  11. game.Players.PlayerRemoving:connect(function(player)
  12.     ds:UpdateAsync(pplayer.Name, function(oldValue) return p.leaderstats.Points.Value end) -- This can be confusing.  The first argument, p.Name, is the name of the key inside the Points DataStore.  The value you update it with has to be a function, so function(oldValue) return value end is the recommended way to go.  The argument of the function is what the value used to be
  13. end)
  14.  
  15. game.OnClose = function() -- This part is recommended to ensure the game saves when the last player leaves the server before it shuts down!
  16.     for i,v in pairs(game.Players:GetChildren()) do
  17.         ds:UpdateAsync(v.Name, function(oldValue) return v.leaderstats.Points.Value end)
  18.     end
  19.     wait(1)
  20. end)
Advertisement
RAW Paste Data Copied
Advertisement