Advertisement
Guest User

DatastoreModule

a guest
Jan 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.08 KB | None | 0 0
  1. local module = {}   -- Handles the game's datastores
  2.  
  3. local DiscordModule = require(script.Parent:WaitForChild("DiscordPost"));
  4.  
  5.  
  6. local Scope = "Testing" -- The scope of our data store
  7.  
  8.  
  9. local DataStoreService = game:GetService("DataStoreService");
  10. local HttpService = game:GetService("HttpService");
  11.  
  12.  
  13. local PlayerDataStore;  -- The player data store that will be retrieved from at the beginning of the game, and updated when needed
  14. local TopKillsStore;
  15. local TopHeadshotsStore;
  16. local TopDeathsStore;
  17.  
  18. local PlayerDataStoreCache = {}; -- The table we'll be updating to throughout the game
  19.  
  20. local Players = game:GetService("Players");
  21.  
  22.  
  23. repeat
  24.     local success,message = pcall(function()       
  25.         PlayerDataStore = DataStoreService:GetDataStore("PlayerData",Scope);    -- Retrieve player data datastore with set scope
  26.         TopKillsStore = DataStoreService:GetOrderedDataStore("TopKills",Scope);
  27.         TopHeadshotsStore = DataStoreService:GetOrderedDataStore("TopHeadshots",Scope);
  28.         TopDeathsStore = DataStoreService:GetOrderedDataStore("TopDeathsStore",Scope);
  29.     end)
  30.     if not success then
  31.         print("Error retrieving PlayerDataStore: "..message);
  32.     end
  33. wait();
  34. until PlayerDataStore and TopKillsStore and TopHeadshotsStore and TopDeathsStore;
  35.  
  36.  
  37. function module.AddKill(client,headshot)
  38.     local success,err = pcall(function()
  39.         TopKillsStore:IncrementAsync(client.UserId,1);
  40.        
  41.         if headshot then
  42.             TopHeadshotsStore:IncrementAsync(client.UserId,1);
  43.         end
  44.     end)
  45. end
  46. function module.AddDeath(client)
  47.     local success,err = pcall(function()
  48.         TopDeathsStore:IncrementAsync(client.UserId,1);
  49.     end)
  50. end
  51.  
  52.  
  53.  
  54. local Leaderboards = Workspace:WaitForChild("Leaderboards");
  55. local TopKillsBoard = Leaderboards:WaitForChild("TopKills");
  56. local TopHeadshotsBoard = Leaderboards:WaitForChild("TopHeadshots");
  57. local TopDeathsBoard = Leaderboards:WaitForChild("TopDeaths");
  58.  
  59. local TopKillsTemplate = script:WaitForChild("TopKillsStats");
  60.  
  61.  
  62. local function GetNameFromUserId(id)
  63.     local success,name = pcall(function()
  64.         local get = HttpService:JSONDecode(HttpService:GetAsync("https://api.rprxy.xyz/Users/"..id));
  65.         if get then
  66.             if get.Username then
  67.                 return get.Username;
  68.             end
  69.         end
  70.     end)
  71.     if success then
  72.         return name;
  73.     else
  74.         return "";
  75.     end
  76. end
  77.  
  78. local function UpdateBoard(board,data)
  79.     local surfacegui = board:WaitForChild("BoardPart"):WaitForChild("SurfaceGui");
  80.     local frame = surfacegui:WaitForChild("Frame"):WaitForChild("RoundedFrame"):WaitForChild("ScrollingFrame");
  81.    
  82.     for _,v in pairs(frame:GetChildren()) do
  83.         if v:isA("Frame") then
  84.             v:Destroy();
  85.         end
  86.     end
  87.    
  88.     pcall(function()
  89.         for rank,v in pairs(data) do
  90.            
  91.             local userid = v.key;
  92.             local kills = v.value;
  93.            
  94.             local name = GetNameFromUserId(userid);
  95.             local template = TopKillsTemplate:clone();
  96.             local killsframe = template:WaitForChild("Kills");
  97.             local playerframe = template:WaitForChild("Player");
  98.             local rankframe = template:WaitForChild("Rank");
  99.            
  100.             rankframe.TextLabel.Text="#"..rank;
  101.             playerframe.PlayerIcon.Image=Players:GetUserThumbnailAsync(userid,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size100x100);
  102.             playerframe.Frame.RoundedFrame.TextLabel.Text=name;
  103.             killsframe.TextLabel.Text=kills;
  104.            
  105.             if name ~= "" then
  106.                 template.LayoutOrder=rank;
  107.                 template.Parent=frame;
  108.             end
  109.         end
  110.     end)
  111. end
  112.  
  113. function module.UpdateLeaderboards()
  114.    
  115.     print("Updating leaderboards");
  116.    
  117.     pcall(function()
  118.         local ordered_topkills = TopKillsStore:GetSortedAsync(false,10):GetCurrentPage();
  119.         local ordered_topdeaths = TopDeathsStore:GetSortedAsync(false,10):GetCurrentPage();
  120.         local ordered_topheadshots = TopHeadshotsStore:GetSortedAsync(false,10):GetCurrentPage();
  121.         UpdateBoard(TopKillsBoard,ordered_topkills);
  122.         UpdateBoard(TopHeadshotsBoard,ordered_topheadshots);
  123.         UpdateBoard(TopDeathsBoard,ordered_topdeaths);
  124.     end)
  125. end
  126.  
  127.  
  128.  
  129. local dataTemplate = {
  130.     OwnedItems={},
  131.     OwnedAbilities={},
  132.     Gold=0,
  133. }
  134.  
  135. local function UpdatePlayerDataToStore(client,newdata)  -- void
  136.     local success,output = pcall(
  137.         function()
  138.            
  139.             print("UPDATING DATASTORE");
  140.             PlayerDataStore:UpdateAsync(client.UserId,
  141.             function(old)
  142.                 return newdata;
  143.             end);
  144.         end
  145.     )
  146.        
  147.     if not success then
  148.         DiscordModule.Post(client,"Error",output,debug.traceback());
  149.     else
  150.         return newdata;
  151.     end
  152. end
  153.  
  154.  
  155.  
  156. local function GetPlayerDataFromStore(client)           -- PlayerData
  157.     local success,output = pcall(
  158.         function()
  159.             print("GETTING DATASTORE");
  160.             return PlayerDataStore:GetAsync(client.UserId) or UpdatePlayerDataToStore(client,dataTemplate); -- Return data or create new and return
  161.         end
  162.     )  
  163.    
  164.     if success then
  165.         if output then
  166.             -- Check if any new indices have been added
  167.             for index,v in pairs(dataTemplate) do
  168.                 if not output[index] then
  169.                     print("Updating with new template");
  170.                     output[index]=dataTemplate[index];
  171.                     UpdatePlayerDataToStore(client,output);
  172.                 end
  173.             end
  174.         end
  175.     end
  176.    
  177.     if not success then
  178.         DiscordModule.Post(client,"Error",output,debug.traceback());
  179.     else
  180.         return output;
  181.     end
  182. end
  183.  
  184.  
  185. local function CreateLocalCache(client) -- Creates cache of player data that will be used, places in PlayerDataStoreCache and returns
  186.    
  187.    
  188.     local success,output = pcall(
  189.         function() 
  190.             return GetPlayerDataFromStore(client);
  191.         end
  192.     )
  193.        
  194.        
  195.     print("Creating local cache");
  196.    
  197.     if success then
  198.         PlayerDataStoreCache[client]=output;
  199.         return output;
  200.     else
  201.         DiscordModule.Post(client,"Error",output,debug.traceback());
  202.     end
  203. end
  204.  
  205. local function GetPlayerData(client)
  206.     local success,output = pcall(
  207.         function()
  208.             return PlayerDataStoreCache[client] or CreateLocalCache(client);
  209.         end
  210.     )
  211.     if success then
  212.         return output;
  213.     else
  214.         DiscordModule.Post(client,"Error",output,debug.traceback());
  215.     end
  216. end
  217.  
  218. module.GetPlayerData=GetPlayerData;
  219.  
  220.  
  221.  
  222.  
  223.  
  224. local function UpdatePlayerData(client) -- Gets player data cache and updates to data store
  225.     local success,output =
  226.         pcall(function()
  227.             return GetPlayerData(client);
  228.         end)
  229.        
  230.    
  231.    
  232.     print("Updating player data: Got Player Data: ",success);
  233.    
  234.     if success then
  235.         UpdatePlayerDataToStore(client,output);
  236.     elseif not success or not output then
  237.         print("Player data is nil!");
  238.         DiscordModule.Post(client,"Error",output or "PlayerData is nil. Cannot update",debug.traceback());
  239.     end
  240. end
  241. module.UpdatePlayerData=UpdatePlayerData;
  242.  
  243.  
  244.  
  245. local function PlayerAdded(plr)
  246.    
  247.     local PlayerData = GetPlayerData();
  248.    
  249. end
  250.  
  251. function module.GivePlayerGold(client,amount)
  252.     local PlayerData = GetPlayerData(client);
  253.     if PlayerData then
  254.        
  255.         if PlayerData.Gold then
  256.            
  257.             PlayerData.Gold=PlayerData.Gold+amount;
  258.             UpdatePlayerData(client);          
  259.            
  260.         else
  261.             DiscordModule.Post(client,"Error","Gold index is nil. Cannot give gold",debug.traceback());
  262.         end
  263.     end
  264. end
  265.  
  266.  
  267. game:GetService("Players").PlayerAdded:connect(PlayerAdded);
  268. for _,v in pairs(game:GetService("Players"):GetPlayers()) do
  269.     PlayerAdded(v);
  270. end
  271.  
  272.  
  273.  
  274. ---- On entry:
  275. --
  276. --PlayerData[plr]=GetPlayerData(plr);
  277. --
  278. ---- On leave:
  279. --
  280. --local data = PlayerData[plr];
  281. --
  282. --if data then
  283. --  UpdatePlayerData(plr,data);
  284. --end
  285. --
  286.  
  287.  
  288.  
  289. return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement