UI bar gone wrong

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

image

Instead of using TweenService use Instance.Size and divide it by MaxHealth and Health

It isn’t a healthbar, it is a level bar

Oh. Misread sorry. It still counts by the samething. MaxLevel to CurrentLevel

That above was a example.

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.

So you want it to start from Size 0 then go to the max correct?

yes, so what I did was minus the xp and max by the xp req of the last level, which does make it start at 0 but it sometimes goes to 150%

You’d always have to check the CurrentLevel goes past the highest level. If so do whatever you’d do

The highest level for now is set extremely high, so that is not a concern

Ooh. So you want the Percentage to be exactly 100%?

yes, between 0 and 100, (x scale of course between 0 and 1)

the preC or whatever math is wrong. Let me find something to assist you.

yes, I was confident that preC was wrong. I multiplied it by 3 because it by its own would only fill 1/3 of the bar

local perC = math.floor((CurrentLevel / MaxLevel) * 100)

Try this

hold on, the percentage is completely fine. It is the tot that was wrong.

Since 150% perC would mean 1.5 x scale,

I didn’t know it was connected to tot i just looked at the preC :sob:

EDIT: meaning I didnt see what you were getting to in tot so I just put it in preC

the bar is meant to signify the progress through that level, not the progress through all of the levels

Right. so the CurrentLevel and the UpcomingMaxLevel