local I=50 local x=game:GetService("\082\101\112\108\105\099\097\116\101\100\083\116\111\114\097\103\101")local u=require((x: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 U=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")local k=u:GetResource("\083\116\097\116\101\046\080\108\097\121\101\114\068\097\116\097")local R=U:Extend()R.CachedLevels={}setmetatable(R.CachedLevels,{__mode="\107"})R:SetClassName("\076\101\118\101\108\115")function R.__new(x,I)x:InitializeSuper()x.PlayerData=k.GetPlayerData(I)x.TotalExperience=x.PlayerData:GetValue("\088\080")x:UpdateLevel();(x.PlayerData:GetValueChangedSignal("\088\080")):Connect(function()x.TotalExperience=x.PlayerData:GetValue("\088\080")x:UpdateLevel()end)end function R.GetLevels(I)if not R.CachedLevels[I]then R.CachedLevels[I]=R.new(I)end return R.CachedLevels[I]end function R.UpdateLevel(x)x.Level=math.floor((-1+math.sqrt(8*(x.TotalExperience/I)+1))/2+1)if x.Level==1 then x.PreviousLevelExperience=0 x.NextLevelExperience=I elseif x.Level==2 then x.PreviousLevelExperience=I x.NextLevelExperience=3*I else x.PreviousLevelExperience=((.5*(x.Level-1))*x.Level)*I x.NextLevelExperience=((.5*x.Level)*(x.Level+1))*I end end return R
--[[
TheNexusAvenger

Calculates levels for players.
--]]

local XP_MULTIPLIER = 50



local ReplicatedStorage = game:GetService("ReplicatedStorage")

local ReplicatedStorageProject = require(ReplicatedStorage:WaitForChild("Project"):WaitForChild("ReplicatedStorage"))

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

local Levels = NexusInstance:Extend()
Levels.CachedLevels = {}
setmetatable(Levels.CachedLevels,{__mode="k"})
Levels:SetClassName("Levels")



--[[
Creates a levels object.
--]]
function Levels:__new(Player)
    self:InitializeSuper()

    --Store the state.
    self.PlayerData = PlayerData.GetPlayerData(Player)
    self.TotalExperience = self.PlayerData:GetValue("XP")
    self:UpdateLevel()

    --Connect the inventory changing.
    self.PlayerData:GetValueChangedSignal("XP"):Connect(function()
        self.TotalExperience = self.PlayerData:GetValue("XP")
        self:UpdateLevel()
    end)
end

--[[
Returns an levels object for a
given player.
--]]
function Levels.GetLevels(Player)
    --Create the cached object if it doesn't exist.
    if not Levels.CachedLevels[Player] then
        Levels.CachedLevels[Player] = Levels.new(Player)
    end

    --Return the cached object.
    return Levels.CachedLevels[Player]
end

--[[
Updates the level for the total XP.
--]]
function Levels:UpdateLevel()
    --Update the level and current level expereince.
    --Current implementation requires level * XP_MULTIPLIER experience per level.
    self.Level = math.floor(((-1 + math.sqrt((8 * (self.TotalExperience / XP_MULTIPLIER) + 1)))/2) + 1)
    if self.Level == 1 then
        self.PreviousLevelExperience = 0
        self.NextLevelExperience = XP_MULTIPLIER
    elseif self.Level == 2 then
        self.PreviousLevelExperience = XP_MULTIPLIER
        self.NextLevelExperience = 3 * XP_MULTIPLIER
    else
        self.PreviousLevelExperience = (0.5 * (self.Level - 1) * self.Level) * XP_MULTIPLIER
        self.NextLevelExperience = (0.5 * self.Level * (self.Level + 1)) * XP_MULTIPLIER
    end
end



return Levels