local H=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local u=require((H:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101"))local z=(require((H:WaitForChild("\080\114\111\106\101\099\116")):WaitForChild("\083\101\114\118\101\114\083\099\114\105\112\116\083\101\114\118\105\099\101"))):GetContext(script)local A=u:GetResource("\083\116\097\116\101\046\080\108\097\121\101\114\068\097\116\097")local Z=u:GetResource("\069\120\116\101\114\110\097\108\085\116\105\108\046\078\101\120\117\115\068\097\116\097\083\116\111\114\101")local W=(u:GetResource("\069\120\116\101\114\110\097\108\085\116\105\108\046\078\101\120\117\115\073\110\115\116\097\110\099\101\046\078\101\120\117\115\073\110\115\116\097\110\099\101")):Extend()W:SetClassName("\080\108\097\121\101\114\068\097\116\097\083\101\114\118\105\099\101")W.PlayerData={}z:SetContextResource(W)local s=z:GetResource("\083\101\114\118\105\099\101\046\065\099\104\105\101\118\101\109\101\110\116\083\101\114\118\105\099\101")function W.LoadPlayer(u,H)local z=Instance.new("\070\111\108\100\101\114")z.Name="\080\108\097\121\101\114\068\097\116\097"z.Parent=H local W={}xpcall(function()local u=Z:GetSaveData(H)u.AutoSaveDelay=10 u:SetSendDataChangeUpdates(false)for H,z in pairs(A.PlayerDataLayout)do W[H]=u:Get(H)end end,function(u)warn("\076\111\097\100\105\110\103 \100\101\102\097\117\108\116 \100\097\116\097 \102\111\114 "..(H.Name..("\059 "..u)))end)u.PlayerData[H]=A.GetPlayerData(H)u.PlayerData[H]:InitializeFromSerializedData(W)local V=u.PlayerData[H];(V:GetValueChangedSignal("\067\111\108\108\101\099\116\101\100\066\108\111\120\107\105\110\115")):Connect(function()if(V:GetValue("\067\111\108\108\101\099\116\101\100\066\108\111\120\107\105\110\115")).Sorcus then s:AwardAchievement(H,"\066\108\111\120\107\105\110\067\111\108\108\101\099\116\111\114")end end)end function W.GetPlayerData(u,H)return u.PlayerData[H]end function W.SavesPlayerData(u,H)xpcall(function()local z=Z:GetSaveData(H)for A,Z in pairs(A.PlayerDataLayout)do z:Set(A,u.PlayerData[H].PlayerData[A])end end,function(u)warn("\078\111\116 \115\097\118\105\110\103 \100\097\116\097 \102\111\114 "..(H.Name..("\059 "..u)))end)end function W.ClearPlayer(u,H)u:SavesPlayerData(H);(Z:GetSaveData(H)):Flush()Z:RemoveFromCache(H)u.PlayerData[H]=nil end return W
--[[
TheNexusAvenger

Stores and replicates player data.
Class is static (should not be created).
--]]

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))
local ServerScriptServiceProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ServerScriptService")):GetContext(script)

local PlayerData = ReplicatedStorageProject:GetResource("State.PlayerData")
local NexusDataStore = ReplicatedStorageProject:GetResource("ExternalUtil.NexusDataStore")

local PlayerDataService = ReplicatedStorageProject:GetResource("ExternalUtil.NexusInstance.NexusInstance"):Extend()
PlayerDataService:SetClassName("PlayerDataService")
PlayerDataService.PlayerData = {}
ServerScriptServiceProject:SetContextResource(PlayerDataService)

local AchievementService = ServerScriptServiceProject:GetResource("Service.AchievementService")



--[[
Initializes the data for a player.
--]]
function PlayerDataService:LoadPlayer(Player)
    --Create the player data folder.
    local PlayerDataFolder = Instance.new("Folder")
    PlayerDataFolder.Name = "PlayerData"
    PlayerDataFolder.Parent = Player
    
    --Initialize the player data.
    local SavedPlayerData = {}
    xpcall(function()
        local DataStore = NexusDataStore:GetSaveData(Player)
        DataStore.AutoSaveDelay = 10
        DataStore:SetSendDataChangeUpdates(false)
        for Key,_ in pairs(PlayerData.PlayerDataLayout) do
            SavedPlayerData[Key] = DataStore:Get(Key)
        end
    end,function(Message)
        --This error happens when a DataStore can't be obtained (not published to Roblox?).
        warn("Loading default data for "..Player.Name.."; "..Message)
    end)
    self.PlayerData[Player] = PlayerData.GetPlayerData(Player)
    self.PlayerData[Player]:InitializeFromSerializedData(SavedPlayerData)

    --Connect the Sorcus bloxkin (last bloxkin) being obtained.
    local LoadedPlayerData = self.PlayerData[Player]
    LoadedPlayerData:GetValueChangedSignal("CollectedBloxkins"):Connect(function()
        if LoadedPlayerData:GetValue("CollectedBloxkins")["Sorcus"] then
            AchievementService:AwardAchievement(Player,"BloxkinCollector")
        end
    end)
end

--[[
Returns the player data object for a player.
--]]
function PlayerDataService:GetPlayerData(Player)
    return self.PlayerData[Player]
end

--[[
Saves the player data for a player.
--]]
function PlayerDataService:SavesPlayerData(Player)
    xpcall(function()
        local DataStore = NexusDataStore:GetSaveData(Player)
        for Key,_ in pairs(PlayerData.PlayerDataLayout) do
            DataStore:Set(Key,self.PlayerData[Player].PlayerData[Key])
        end
    end,function(Message)
        --This error happens when a DataStore can't be obtained (not published to Roblox?).
        warn("Not saving data for "..Player.Name.."; "..Message)
    end)
end

--[[
Clears a player.
--]]
function PlayerDataService:ClearPlayer(Player)
    --Save the player data.
    self:SavesPlayerData(Player)
    NexusDataStore:GetSaveData(Player):Flush()
    NexusDataStore:RemoveFromCache(Player)

    --Clear the resources.
    self.PlayerData[Player] = nil
end



return PlayerDataService