Guest User

Untitled

a guest
May 4th, 2019
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.40 KB | None | 0 0
  1. --TimScript
  2. local dataStore = {}
  3. local dataStoreService = game:GetService("DataStoreService")
  4. local httpService = game:GetService("HttpService")
  5. local ssService = game:GetService("ServerScriptService")
  6. local runService = game:GetService("RunService")
  7. local Id = require(ssService.SetIds.Id)
  8. local leaderstatsStore = dataStoreService:GetDataStore("Leaderstats")
  9. local itemsStore = dataStoreService:GetDataStore("Items")
  10. local itemCache = {}
  11. local statsCache = {}
  12.  
  13. local function tableFind(t,f)
  14. for _,v in pairs(t) do
  15. if v == f then
  16. return true
  17. end
  18. end
  19. return false
  20. end
  21. -------------------------------------------------
  22. function dataStore:SaveLeaderstats(plr)
  23. if runService:IsStudio() then return end
  24. local playerKey = "plr_" .. plr.UserId
  25.  
  26. local toSave = dataStore:GetCachedStats(plr)
  27.  
  28. if not toSave then
  29. toSave = {}
  30. for _,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
  31. table.insert(toSave,{v.Name,v.Value})
  32. end
  33. end
  34.  
  35. local success, err = pcall(function()
  36. leaderstatsStore:SetAsync(playerKey,httpService:JSONEncode(toSave))
  37. end)
  38. if err then warn(err) end
  39. end
  40.  
  41. function dataStore:GetLeaderstats(plr)
  42. if runService:IsStudio() then return end
  43. local playerKey = "plr_" .. plr.UserId
  44. local toGet
  45.  
  46. local success, err = pcall(function()
  47. toGet = (leaderstatsStore:GetAsync(playerKey))
  48. end)
  49. if err then warn(err) end
  50.  
  51. if success and toGet then
  52. return httpService:JSONDecode(toGet)
  53. end
  54. return {}
  55. end
  56. -------------------------------------------------
  57. function dataStore:CacheStats(plr)
  58. local playerCache = {}
  59. for _,v in pairs(plr:WaitForChild("leaderstats"):GetChildren()) do
  60. table.insert(playerCache,{v.Name,v.Value})
  61. end
  62.  
  63. statsCache[plr.UserId] = playerCache
  64.  
  65. print("Cached: "..plr.Name.."'s stats")
  66. end
  67.  
  68. function dataStore:RemoveCachedStats(plr)
  69. statsCache[plr.UserId] = nil
  70. end
  71.  
  72. function dataStore:GetCachedStats(plr)
  73. return statsCache[plr.UserId]
  74. end
  75. -------------------------------------------------
  76. function dataStore:SaveItems(plr,fast)
  77. if runService:IsStudio() then return end
  78. local playerKey = "plr_" .. plr.UserId
  79. local char
  80.  
  81. if not fast then
  82. char = plr.Character or plr.CharacterAdded:Wait()
  83. end
  84.  
  85. local toSave = {}
  86.  
  87. if itemCache[plr.UserId] then
  88. local cache = itemCache[plr.UserId]
  89. for _,v in pairs(cache) do
  90. table.insert(toSave,v)
  91. end
  92. else
  93. for _,v in pairs(plr.Backpack:GetChildren()) do
  94. table.insert(toSave,v.Id.Value)
  95. end
  96. if char and char:FindFirstChildWhichIsA("Tool") then
  97. table.insert(toSave,char:FindFirstChildWhichIsA("Tool").Id.Value)
  98. end
  99. end
  100.  
  101.  
  102. local success, err = pcall(function()
  103. itemsStore:UpdateAsync(playerKey,function(old)
  104. local oldValue = httpService:JSONDecode(old or {})
  105.  
  106. local hash = {}
  107. local newValue = {}
  108.  
  109. for _,v in ipairs(oldValue) do
  110. if not hash[v] then
  111. table.insert(newValue,v)
  112. hash[v] = true
  113. end
  114. end
  115. return httpService:JSONEncode(newValue)
  116. end)
  117. print("Saved: "..plr.Name.."'s items")
  118. end)
  119. if err then warn(err) end
  120. end
  121.  
  122. function dataStore:GetItems(plr)
  123. if runService:IsStudio() then return end
  124. local playerKey = "plr_" .. plr.UserId
  125. local ids
  126.  
  127. local success, err = pcall(function()
  128. ids = itemsStore:GetAsync(playerKey)
  129. end)
  130. if err then warn(err) end
  131.  
  132. if success and ids then
  133. local toGet = {}
  134.  
  135. for _,v in pairs(httpService:JSONDecode(ids)) do
  136. if not tableFind(toGet, Id[tonumber(v)]) then
  137. table.insert(toGet, Id[tonumber(v)])
  138. end
  139. end
  140.  
  141. return toGet
  142. end
  143. return {}
  144. end
  145. -------------------------------------------------
  146. function dataStore:CacheItems(plr,t,fast)
  147. local playerCache = {}
  148. local char
  149.  
  150. if not fast then
  151. char = plr.Character or plr.CharacterAdded:Wait()
  152. end
  153.  
  154. if t then
  155. for i,v in pairs(t) do
  156. table.insert(playerCache,v:WaitForChild("Id").Value)
  157. end
  158. else
  159. for _,v in pairs(plr.Backpack:GetChildren()) do
  160. table.insert(playerCache,v:WaitForChild("Id").Value)
  161. end
  162. if char and char:FindFirstChildWhichIsA("Tool") then
  163. table.insert(playerCache,char:FindFirstChildWhichIsA("Tool"):WaitForChild("Id").Value)
  164. end
  165. end
  166.  
  167. itemCache[plr.UserId] = playerCache
  168.  
  169. print("Cached: "..plr.Name.."'s items")
  170. end
  171.  
  172. function dataStore:GetCachedItems(plr)
  173. local toGet = {}
  174.  
  175. for i,v in pairs(itemCache[plr.UserId]) do
  176. table.insert(toGet,Id[v])
  177. end
  178.  
  179. return toGet
  180. end
  181.  
  182. return dataStore
Advertisement
Add Comment
Please, Sign In to add comment