Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local module = {} -- Handles the game's datastores
- local DiscordModule = require(script.Parent:WaitForChild("DiscordPost"));
- local Scope = "Testing" -- The scope of our data store
- local DataStoreService = game:GetService("DataStoreService");
- local HttpService = game:GetService("HttpService");
- local PlayerDataStore; -- The player data store that will be retrieved from at the beginning of the game, and updated when needed
- local TopKillsStore;
- local TopHeadshotsStore;
- local TopDeathsStore;
- local PlayerDataStoreCache = {}; -- The table we'll be updating to throughout the game
- local Players = game:GetService("Players");
- repeat
- local success,message = pcall(function()
- PlayerDataStore = DataStoreService:GetDataStore("PlayerData",Scope); -- Retrieve player data datastore with set scope
- TopKillsStore = DataStoreService:GetOrderedDataStore("TopKills",Scope);
- TopHeadshotsStore = DataStoreService:GetOrderedDataStore("TopHeadshots",Scope);
- TopDeathsStore = DataStoreService:GetOrderedDataStore("TopDeathsStore",Scope);
- end)
- if not success then
- print("Error retrieving PlayerDataStore: "..message);
- end
- wait();
- until PlayerDataStore and TopKillsStore and TopHeadshotsStore and TopDeathsStore;
- function module.AddKill(client,headshot)
- local success,err = pcall(function()
- TopKillsStore:IncrementAsync(client.UserId,1);
- if headshot then
- TopHeadshotsStore:IncrementAsync(client.UserId,1);
- end
- end)
- end
- function module.AddDeath(client)
- local success,err = pcall(function()
- TopDeathsStore:IncrementAsync(client.UserId,1);
- end)
- end
- local Leaderboards = Workspace:WaitForChild("Leaderboards");
- local TopKillsBoard = Leaderboards:WaitForChild("TopKills");
- local TopHeadshotsBoard = Leaderboards:WaitForChild("TopHeadshots");
- local TopDeathsBoard = Leaderboards:WaitForChild("TopDeaths");
- local TopKillsTemplate = script:WaitForChild("TopKillsStats");
- local function GetNameFromUserId(id)
- local success,name = pcall(function()
- local get = HttpService:JSONDecode(HttpService:GetAsync("https://api.rprxy.xyz/Users/"..id));
- if get then
- if get.Username then
- return get.Username;
- end
- end
- end)
- if success then
- return name;
- else
- return "";
- end
- end
- local function UpdateBoard(board,data)
- local surfacegui = board:WaitForChild("BoardPart"):WaitForChild("SurfaceGui");
- local frame = surfacegui:WaitForChild("Frame"):WaitForChild("RoundedFrame"):WaitForChild("ScrollingFrame");
- for _,v in pairs(frame:GetChildren()) do
- if v:isA("Frame") then
- v:Destroy();
- end
- end
- pcall(function()
- for rank,v in pairs(data) do
- local userid = v.key;
- local kills = v.value;
- local name = GetNameFromUserId(userid);
- local template = TopKillsTemplate:clone();
- local killsframe = template:WaitForChild("Kills");
- local playerframe = template:WaitForChild("Player");
- local rankframe = template:WaitForChild("Rank");
- rankframe.TextLabel.Text="#"..rank;
- playerframe.PlayerIcon.Image=Players:GetUserThumbnailAsync(userid,Enum.ThumbnailType.HeadShot,Enum.ThumbnailSize.Size100x100);
- playerframe.Frame.RoundedFrame.TextLabel.Text=name;
- killsframe.TextLabel.Text=kills;
- if name ~= "" then
- template.LayoutOrder=rank;
- template.Parent=frame;
- end
- end
- end)
- end
- function module.UpdateLeaderboards()
- print("Updating leaderboards");
- pcall(function()
- local ordered_topkills = TopKillsStore:GetSortedAsync(false,10):GetCurrentPage();
- local ordered_topdeaths = TopDeathsStore:GetSortedAsync(false,10):GetCurrentPage();
- local ordered_topheadshots = TopHeadshotsStore:GetSortedAsync(false,10):GetCurrentPage();
- UpdateBoard(TopKillsBoard,ordered_topkills);
- UpdateBoard(TopHeadshotsBoard,ordered_topheadshots);
- UpdateBoard(TopDeathsBoard,ordered_topdeaths);
- end)
- end
- local dataTemplate = {
- OwnedItems={},
- OwnedAbilities={},
- Gold=0,
- }
- local function UpdatePlayerDataToStore(client,newdata) -- void
- local success,output = pcall(
- function()
- print("UPDATING DATASTORE");
- PlayerDataStore:UpdateAsync(client.UserId,
- function(old)
- return newdata;
- end);
- end
- )
- if not success then
- DiscordModule.Post(client,"Error",output,debug.traceback());
- else
- return newdata;
- end
- end
- local function GetPlayerDataFromStore(client) -- PlayerData
- local success,output = pcall(
- function()
- print("GETTING DATASTORE");
- return PlayerDataStore:GetAsync(client.UserId) or UpdatePlayerDataToStore(client,dataTemplate); -- Return data or create new and return
- end
- )
- if success then
- if output then
- -- Check if any new indices have been added
- for index,v in pairs(dataTemplate) do
- if not output[index] then
- print("Updating with new template");
- output[index]=dataTemplate[index];
- UpdatePlayerDataToStore(client,output);
- end
- end
- end
- end
- if not success then
- DiscordModule.Post(client,"Error",output,debug.traceback());
- else
- return output;
- end
- end
- local function CreateLocalCache(client) -- Creates cache of player data that will be used, places in PlayerDataStoreCache and returns
- local success,output = pcall(
- function()
- return GetPlayerDataFromStore(client);
- end
- )
- print("Creating local cache");
- if success then
- PlayerDataStoreCache[client]=output;
- return output;
- else
- DiscordModule.Post(client,"Error",output,debug.traceback());
- end
- end
- local function GetPlayerData(client)
- local success,output = pcall(
- function()
- return PlayerDataStoreCache[client] or CreateLocalCache(client);
- end
- )
- if success then
- return output;
- else
- DiscordModule.Post(client,"Error",output,debug.traceback());
- end
- end
- module.GetPlayerData=GetPlayerData;
- local function UpdatePlayerData(client) -- Gets player data cache and updates to data store
- local success,output =
- pcall(function()
- return GetPlayerData(client);
- end)
- print("Updating player data: Got Player Data: ",success);
- if success then
- UpdatePlayerDataToStore(client,output);
- elseif not success or not output then
- print("Player data is nil!");
- DiscordModule.Post(client,"Error",output or "PlayerData is nil. Cannot update",debug.traceback());
- end
- end
- module.UpdatePlayerData=UpdatePlayerData;
- local function PlayerAdded(plr)
- local PlayerData = GetPlayerData();
- end
- function module.GivePlayerGold(client,amount)
- local PlayerData = GetPlayerData(client);
- if PlayerData then
- if PlayerData.Gold then
- PlayerData.Gold=PlayerData.Gold+amount;
- UpdatePlayerData(client);
- else
- DiscordModule.Post(client,"Error","Gold index is nil. Cannot give gold",debug.traceback());
- end
- end
- end
- game:GetService("Players").PlayerAdded:connect(PlayerAdded);
- for _,v in pairs(game:GetService("Players"):GetPlayers()) do
- PlayerAdded(v);
- end
- ---- On entry:
- --
- --PlayerData[plr]=GetPlayerData(plr);
- --
- ---- On leave:
- --
- --local data = PlayerData[plr];
- --
- --if data then
- -- UpdatePlayerData(plr,data);
- --end
- --
- return module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement