Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DSService = game:GetService('DataStoreService'):GetDataStore('NAME')
- ID = 6
- SaveTime = 300
- leaderboardname = "Stats"
- local numberOfRetries = 5
- StatList = { }
- function CreateValue(Name, Parent, Value)
- Value = Value or 0
- local savevalue = Instance.new('IntValue', Parent)
- savevalue.Name = Name
- savevalue.Value = Value
- return savevalue
- end
- function GetValue(uniquekey)
- -- GetAsync
- local count = 0
- local Success, GetSaved repeat
- Success, GetSaved = pcall(function() return DSService:GetAsync(uniquekey) end)
- count = count + 1
- wait(1.5)
- until Success or count == numberOfRetries
- if Sucess and GetSaved then
- return GetSaved
- end
- return nil
- end
- game.Players.PlayerAdded:connect(function(plr)
- local uniquekey = ID..plr.userId
- local Levelss = Instance.new('IntValue', plr)
- Levelss.Name = leaderboardname
- for i,v in pairs(StatList) do
- local Stat = GetValue(i..uniquekey)
- if Stat then
- CreateValue(i, Levelss, Stat) --Old player load their value
- else
- CreateValue(i, Levelss, v) --New player load the default value
- end
- end
- end)
- game.Players.PlayerRemoving:connect(function(plr)
- local uniquekey = ID..plr.userId
- if plr:FindFirstChild(leaderboardname) then
- for i,v in pairs(StatList) do
- if plr[leaderboardname]:FindFirstChild(i) then
- DSService:SetAsync(i..uniquekey, plr[leaderboardname][i].Value)
- end
- end
- end
- end)
- while wait(SaveTime) do
- P = game:GetService("Players"):GetChildren()
- for o=1, #P do
- if P[o] then
- local uniquekey = ID..P[o].userId
- if P[o]:FindFirstChild(leaderboardname) then
- for i,v in pairs(StatList) do
- if P[o][leaderboardname]:FindFirstChild(i) then
- local count = 0
- local Success repeat
- Success = pcall(function() DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value) end)
- count = count + 1
- wait(1.5)
- until Success or count == numberOfRetries
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement