Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DSService = game:GetService('DataStoreService'):GetDataStore('DataStorageName')
- ID = 3 --If you change this then it will reset everyones stats.
- SaveTime = 60 --Shouldn't be less then 60
- leaderboardname = "Stats" --The name of the leader board
- StatList = {Stat = 0}
- --StartingMoney would give new players the stat StartingMoney with the value 5
- --Put any of your values here
- --Values are loaded when the player joins and saved when they exit
- --Values are also saved every SaveTime seconds
- ----- You shouldn't need to change anything below this line -----
- function CreateValue(Name, Parent, Value) --If value is set to a number then new players will start with that amount.
- Value = Value or 0
- local savevalue = Instance.new('IntValue', Parent)
- savevalue.Name = Name
- savevalue.Value = Value
- return savevalue
- end
- function GetValue(uniquekey)
- -- GetAsync
- local GetSaved = DSService:GetAsync(uniquekey)
- if GetSaved then
- return GetSaved
- end
- end
- game.Players.PlayerAdded:connect(function(plr)
- local uniquekey = ID..plr.userId
- local Stats = Instance.new('IntValue', plr)
- Stats.Name = leaderboardname
- for i,v in pairs(StatList) do
- local Stat = GetValue(i..uniquekey)
- if Stat then
- CreateValue(i, Stats, Stat) --Old player load their value
- else
- CreateValue(i, Stats, 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
- DSService:SetAsync(i..uniquekey, P[o][leaderboardname][i].Value)
- end
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement