Hey! I have been making a GUI xp bar, and I realised that the bar, for late levels, goes to 150%, whilst the lower levels are fine
I am not sure what the error it, but here is the script:
local LevelM = require(game.ReplicatedStorage.Levels)
local UserXP = game.ReplicatedStorage.PlayerData:WaitForChild(game.Players.LocalPlayer.UserId).Xp
local Levels = LevelM.Levels
local SB = script.Parent
local InfText = script.Parent.Parent.Parent.InfText
while true do task.wait(0.1)
local xp = UserXP.Value
local UserLevel = LevelM.ConvertToLevel(xp)
local toS = 0
local NextLev = Levels[UserLevel+1]
if UserLevel > 1 then
toS = Levels[UserLevel-1]
end
local tot = ((xp-toS)/(NextLev-toS))*3
local perC = math.round(tot*100)
InfText.Text = "Level "..UserLevel.." ("..perC.."%)"
SB:TweenSize(UDim2.fromScale(tot,1),Enum.EasingDirection.Out,Enum.EasingStyle.Linear,.55)
end
This is the LevelModule:
local Module = {}
Module.Levels = {100,250,500,1000,1500,2000,4000,7500,10000,15000,20000,50000,100000,250000,500000,1000000,1500000,2500000,5000000,10000000}
Module.ConvertToLevel = function(xp)
local TargetLev = 0
for i, v in pairs(Module.Levels) do
if xp <= v-1 then
TargetLev = i
break
end
end
return TargetLev
end
return Module
CurrentLevel is a number. If I want the MaxHealth equavilant to be CurrentLevel, then the xp from the levels before would be counted and it wouldn’t start at the start of the bar.