Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ds = game:GetService("DataStoreService"):GetDataStore("Points") -- GetDataStore provides a key to save everything under. Can be anything.
- game.Players.PlayerAdded:connect(function(player)
- local leaderstats = Instance.new("IntValue",player) -- Creating the leaderboard
- leaderstats.Name = "leaderstats"
- local points = Instance.new("IntValue",player)
- points.Name = "Points"
- 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.
- end)
- game.Players.PlayerRemoving:connect(function(player)
- 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
- end)
- game.OnClose = function() -- This part is recommended to ensure the game saves when the last player leaves the server before it shuts down!
- for i,v in pairs(game.Players:GetChildren()) do
- ds:UpdateAsync(v.Name, function(oldValue) return v.leaderstats.Points.Value end)
- end
- wait(1)
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement