Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.35 KB | None | 0 0
  1. repeat wait() until _G.DataStoreName2 ~= nil
  2. local RatingDataCache = {}
  3. local Connections = {}
  4. local RemoteEvent = script.Parent.Parent:WaitForChild("UpdatedCache")
  5. local DataStore = game:GetService('DataStoreService'):GetOrderedDataStore(_G.DataStoreName2)
  6.  
  7. function ParseKey(key,value)
  8. local itemid = key:match("Rating_(.+)")
  9. local index
  10. if itemid then
  11. index = "Rating"
  12. else
  13. itemid = key:match("Raters_(.+)")
  14. if itemid then
  15. index = "Raters"
  16. end
  17. end
  18. RatingDataCache[itemid] = RatingDataCache[itemid] or {}
  19. print(key,value)
  20. RatingDataCache[itemid][index] = value
  21. end
  22.  
  23. function SendUpdateToClients()
  24. RemoteEvent:FireAllClients(false, true)
  25. end
  26.  
  27. function GetRatingDataForCache()
  28. local pages = DataStore:GetSortedAsync(true, 100)
  29. while wait() do
  30. for i, item in ipairs(pages:GetCurrentPage()) do
  31. ParseKey(item.key,item.value)
  32. if not Connections[item.key] then
  33. Connections[item.key] = DataStore:OnUpdate(item.key,function(value)
  34. ParseKey(item.key,value)
  35. SendUpdateToClients()
  36. end)
  37. end
  38. end
  39. if pages.IsFinished then
  40. break
  41. end
  42. pages:AdvanceToNextPageAsync()
  43. end
  44. SendUpdateToClients()
  45. end
  46.  
  47. spawn(function()
  48. while wait(15) do
  49. GetRatingDataForCache()
  50. end
  51. end)
  52.  
  53. return function()
  54. return RatingDataCache
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement