How do i fix leveling up gui

So everytime i level up the gui gets all bigg and im not sure how to fix it

local player = game.Players.LocalPlayer
local CurrentExp = player:WaitForChild("leaderstats").Exp
local MaxExp = player:WaitForChild("leaderstats").ExpToLevel

local Gui = script.Parent
local Exterior = Gui:WaitForChild("BackFrame")
local label = Gui:WaitForChild("TextLabel")
local Bar = Gui:WaitForChild("Expbar")


script.Parent.TextLabel.Text = CurrentExp.Value.."/"..MaxExp.Value
CurrentExp.Changed:Connect(function()
	script.Parent.TextLabel.Text = CurrentExp.Value.."/"..MaxExp.Value
	if CurrentExp.Value >= MaxExp.Value then
		Bar.Size = UDim2.new(CurrentExp.Value/MaxExp.Value,0,1,0)
		wait(0.5)
		CurrentExp.Value = 0
		Bar.Size = UDim2.new(CurrentExp.Value/MaxExp.Value,0,1,0)
	else
		Bar.Size = UDim2.new(CurrentExp.Value/MaxExp.Value,0,1,0)
	end
end)

Why do you have the wait(0.5), I think it would work if you just did:

script.Parent.TextLabel.Text = CurrentExp.Value.."/"..MaxExp.Value
CurrentExp.Changed:Connect(function()
	script.Parent.TextLabel.Text = CurrentExp.Value.."/"..MaxExp.Value
	if CurrentExp.Value >= MaxExp.Value then
		CurrentExp.Value = 0
		Bar.Size = UDim2.new(CurrentExp.Value/MaxExp.Value,0,1,0)
	else
		Bar.Size = UDim2.new(CurrentExp.Value/MaxExp.Value,0,1,0)
	end
end)

Make sure that black holder is the parent of that purple exp bar.

because if there was no wait then
and the purple bar would stay there for ever

Hello, the progress bar becomes big because your currentExp is a thousand times larger than the MaxExp. If you want to fix this, just avoid making the currentExp value a lot bigger than the MaxExp value.

Or to highlight an answer from one of my threads, you can just use math.clamp() on the bar.

UDim2.new(math.clamp(currentExp, 0, MaxExp) / MaxExp, 0, 1, 0)

doesn’t work

Are here any errors? Also make sure sure add .Value in the current exp and max exp because I forgot to.

yes i made sure that i added .value but it still doesn’t work

What exactly is still happening? Are there any errors or is the bar still getting big?

This is what is happening

Instead of adding .Changed just add while loop.
If that doesn’t work then pls show me the gui object in your explorer.

script.Parent.TextLabel.Text = CurrentExp.Value.."/"..MaxExp.Value
while wait() do
	script.Parent.TextLabel.Text = CurrentExp.Value.."/"..MaxExp.Value
	if CurrentExp.Value >= MaxExp.Value then
		CurrentExp.Value = 0
		Bar.Size = UDim2.new(CurrentExp.Value/MaxExp.Value,0,1,0)
	else
		Bar.Size = UDim2.new(CurrentExp.Value/MaxExp.Value,0,1,0)
	end
end

That doesn’t work either

Show me the gui in your explorer.

Screen Shot 2021-03-10 at 7.48.18 AM

Thats why! put the expBar in BackFrame. Since 1,0,1,0 will scale accounding to there parent size

And the parent of the Expbar is ScreenGui(entire screen)

Did you replace every UDim2? In my experience, it should work because math.clamp accepts three values; (num, min, and max) and if the num exceeds the max, it will always return max. Other than that I don’t know the problem.

Hello, I don’t think using while wait() do makes a difference from .Changed, while wait() do just causes the script to run every 0.03 seconds even when it is not needed and when the user experience remains stationary. However, using .Changed will only change the bar when the user experience updates.