Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local DS = game:GetService("DataStoreService"):GetGlobalDataStore("PlayerDataStore")
- function SaveData(Key, Value)
- DS:UpdateAsync(Key, function(OldValue)
- return Value
- end)
- end
- function GetSaveTable(StatsModel)
- local R = {}
- for i,v in pairs(StatsModel:GetChildren()) do
- R[tostring(i.Name)] = v.Value
- end
- return R
- end
- game.Players.PlayerAdded:connect(function(Player)
- local PlayerDataStore = DS:GetAsync(Player.userId)
- local stats = Instance.new("Model", game.ReplicatedStorage.PlayerStats)
- -- Throttle DataStore updates
- local Request_WaitSecondsThreshold = 30 -- Save every 30 seconds
- local Request_QueueThreshold = 5 -- or save whenever 5 changes have happened
- local CurrentRequestQueue = 0
- -- The above is to make sure we don't hit the DataStore request limit
- stats.Name = Player.Name
- -- If they don't have a save, make one.
- if PlayerDataStore then
- PlayerDataStore = {Exp = 0, Gold = 0, Level = 1, Levelcap = 120, HP = 83, MaxExp = 83, Stamina = 100, MaxHP = 85}
- print("PDS made")
- end
- for x,v in pairs(PlayerDataStore) do
- local Num = Instance.new("IntValue",stats)
- Num.Name = x
- Num.Value = v
- Num.Changed:connect(function(Exp)
- CurrentRequestQueue = CurrentRequestQueue + 1
- if (CurrentRequestQueue >= Request_QueueThreshold) then
- local Data = GetSaveTable(stats)
- CurrentRequestQueue = 0
- SaveData(Data)
- end
- end)
- end
- while (wait(Request_WaitSecondsThreshold)) do
- local Data = GetSaveTable(stats)
- CurrentRequestQueue = 0
- SaveData(Data)
- end
- end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement