Exp bar extending too far

Im not sure why this is happening, can someone help me please

local bar = script.Parent.BackFrame.Expbar

local text = script.Parent.TextLabel

local player = game.Players.LocalPlayer

local level = player.leaderstats.Level

local expStat = player.leaderstats.Exp

local expNeeded = tonumber(100 * math.exp(level.Value))

text.Text = expStat.Value.."/"..expNeeded

local change = expStat.Value / expNeeded

bar:TweenSize(UDim2.new(change,0,1,0),"In","Linear",0.5)

expStat.Changed:Connect(function()

expNeeded = tonumber(100 + math.exp(level.Value * 2))^level.Value

text.Text = expStat.Value.."/"..expNeeded

local change = expStat.Value / expNeeded

bar:TweenSize(UDim2.new(change,0,1,0), "In","Linear",0.5)

end)

level.Changed:Connect(function()

expNeeded = tonumber(100 + math.exp(level.Value * 2))^level.Value

text.Text = expStat.Value.."/"..expNeeded

local change = expStat.Value / expNeeded

bar:TweenSize(UDim2.new(change,0,1,0), "In","Linear",0.5)

end)

Screen Shot 2021-03-25 at 3.59.14 PM

Where local change is, try putting this here instead.

local change = 0

if expStat.Value > expNeeded then
    change = 1
else
    change = expStat.Value / expNeeded
end

If expStat is greater than expNeeded then the change value will be greater than 1 and it’ll go over the bar.

Its working but when it reaches the full bar, the bar doesn’t reset back

Where does expStat and the level variable get changed? It should only remain full if expStat is greater than expNeeded.

oh, how can i make it so it resets?

very simple

You would have to use an on changed event (on the server) to check if the exp is greater than the expneeded. If it is, set the exp value to 0 and set the level to itself plus one.

Your issue lies in that you are setting the raw value of the expStat.Value / expNeeded to the bar’s scale value. Try doing,

change = expStat.Value / expNeeded
change = change * barScale

Where barScale is how big you want the bar’s scale to be at 100% xp.

but how do i tween the exp to go lower

barscale is unknown?

Where barScale is how big you want the bar’s scale to be at 100% xp. How big you want it to be, that is the value of barScale.

but im not sure what to set that value to

If you want the bar to only be a quarter of the width of the screen, set it to 0.25. If you want it to be half the screen, use 0.50.

what about full?

This is what happens

…barScale is the size of the xp that you want it to be when your xp is completely full.

oh alright. Why wont the exp update after it reached maxed

Could we see the updated script?

local bar = script.Parent.Bruh.Expbar

local text = script.Parent.TextLabel

local player = game.Players.LocalPlayer

local level = player.leaderstats.Level

local expStat = player.leaderstats.Exp

local expNeeded = tonumber(100 * math.exp(level.Value))
local barScale = 0.50

text.Text = expStat.Value.."/"..expNeeded

local change = 0

if expStat.Value > expNeeded then
	change = 1
else
	change = expStat.Value / expNeeded
	change = change * barScale
end
bar:TweenSize(UDim2.new(change,0,1,0),"In","Linear",0.5)

expStat.Changed:Connect(function()

	expNeeded = tonumber(100 + math.exp(level.Value * 2))^level.Value

	text.Text = expStat.Value.."/"..expNeeded

	local change = expStat.Value / expNeeded

	bar:TweenSize(UDim2.new(change,0,1,0), "In","Linear",0.5)

end)

level.Changed:Connect(function()

	expNeeded = tonumber(100 + math.exp(level.Value * 2))^level.Value

	text.Text = expStat.Value.."/"..expNeeded

	local change = expStat.Value / expNeeded

	bar:TweenSize(UDim2.new(change,0,1,0), "In","Linear",0.5)

end)

Could be a number of reasons why the exp bar doesn’t reset. Could be that you’re not properly updating the player’s level, could also be that you are running multiple tween’s on the exp bar, but none of them override each other.