Wont size bar correctly

I am trying to make a level bar but for some reason when ever the player reaches the required xp for the next level the bar stays the size (1,0,1,0) instead of (0,0,0,0). Here is the funtion:

function Change()
	local currentLvl = plr:WaitForChild("leaderstats").Level.Value
	local currentExp = plr:WaitForChild("leaderstats").Exp.Value
	print(currentExp.." : Exp -- Level : "..currentLvl) --Prints 0 :Exp -- Level : 14
	level.Text = "Level "..currentLvl
	xp.Text = currentExp.."/"..currentLvl*25
	
	bar:TweenSize(UDim2.new(currentExp/(currentLvl*25),0,1,0),"Out","Quad",.3)
	
	if bar.Size == UDim2.new(nil,0,1,0)then
		bar.Size = UDim2.new(currentExp/(currentLvl*25),0,1,0)
	end
	task.wait(.3)
	if currentExp >= currentLvl*25 then
		script.NextLevel:FireServer(currentExp,currentLvl) -- This will cause funtion to run again after level is changed
	end
end

I am 100% sure that the function runs since it prints something in the output but the sizing of the bar is incorrect. Any help is appreciated :slight_smile:

1 Like
if currentExp >= currentLvl*25 then
        currentExp = 0 -- This will make the EXP turn back to 0.
		script.NextLevel:FireServer(currentExp,currentLvl) -- This will cause funtion to run again after level is changed
end

Maybe you can try this.

2 Likes

The problem is the size

the currentExp is equal to 0 and 0 divided by anything is 0 so this should be equal to Udim2.new(0,0,1,0) but it is Udmi2.new(1,0,1,0)

Edit: I added a print before it to check and it printed that currentExp = 0

Are there any underlined errors in the code or in the Script Analysis tab? Or any errors in the output?

No errors are appearing(character limit)

Try using TweenService.

TweenService:Create(
    bar,
    TweenInfo.new(0.3),
    {
        Size = UDim2.new(currentExp/(currentLvl*25), 0, 1, 0)
    }
)

This check is not necessary.

Also, you seem to be sending the server your current experience level. This is bad!
The client should tell the server if it’s performed an action that awards experience points, and the server should handle awarding the player.