Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.32 KB | None | 0 0
  1. local DSService = game:GetService('DataStoreService'):GetDataStore('DataStorageName')
  2. ID = 3 --If you change this then it will reset everyones stats.
  3. SaveTime = 60 --Shouldn't be less then 60
  4. leaderboardname = "Stats" --The name of the leader board
  5.  
  6. StatList = {Stat = 0}
  7. --StartingMoney would give new players the stat StartingMoney with the value 5
  8. --Put any of your values here
  9. --Values are loaded when the player joins and saved when they exit
  10. --Values are also saved every SaveTime seconds
  11.  
  12. ----- You shouldn't need to change anything below this line -----
  13.  
  14. function CreateValue(Name, Parent, Value) --If value is set to a number then new players will start with that amount.
  15.     Value = Value or 0
  16.     local savevalue =  Instance.new('IntValue', Parent)
  17.     savevalue.Name = Name
  18.     savevalue.Value = Value
  19.     return savevalue
  20. end
  21.  
  22. function GetValue(uniquekey)
  23.     -- GetAsync
  24.     local GetSaved = DSService:GetAsync(uniquekey)
  25.     if GetSaved then
  26.         return GetSaved
  27.     end
  28. end
  29.  
  30. game.Players.PlayerAdded:connect(function(plr)
  31.     local uniquekey = ID..plr.userId
  32.     local Stats = Instance.new('IntValue', plr)
  33.     Stats.Name = leaderboardname
  34.     for i,v in pairs(StatList) do
  35.         local Stat = GetValue(i..uniquekey)
  36.         if Stat then
  37.             CreateValue(i, Stats, Stat) --Old player load their value
  38.         else
  39.             CreateValue(i, Stats, v) --New player load the default value
  40.         end
  41.     end
  42. end)
  43.  
  44. game.Players.PlayerRemoving:connect(function(plr)
  45.     local uniquekey = ID..plr.userId
  46.     if plr:FindFirstChild(leaderboardname) then
  47.         for i,v in pairs(StatList) do
  48.             if plr[leaderboardname]:FindFirstChild(i) then
  49.                 DSService:SetAsync(i..uniquekey, plr[leaderboardname][i].Value)
  50.             end
  51.         end
  52.     end
  53. end)
  54.  
  55. while wait(SaveTime) do
  56.     P = game:GetService("Players"):GetChildren()
  57.     for o=1, #P do
  58.         if P[o] then
  59.             local uniquekey = ID..P[o].userId
  60.             if P[o]:FindFirstChild(leaderboardname) then
  61.                 for i,v in pairs(StatList) do
  62.                     if P[o][leaderboardname]:FindFirstChild(i) then
  63.                         DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value)
  64.                     end
  65.                 end
  66.             end
  67.         end
  68.     end
  69. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement