Exp gui not updating maxExp after leveling up until getting experience again

Hi, My problem currently is that I have leveling system connected to a Gui I made. The Gui updates to show what level you are and the current xp/max xp as well as moves the experience bar along as you get more experience. It’s calculating overflow experience and everything perfectly fine and it’s functional, BUT when the player levels up from say level 1 to level 2. If level 1’s exp bar was 49/50 where 50 experience is what is needed, and the level 2 exp bar is 0/100, as soon as they level up the Gui updates to show their new level and their current exp fine but it doesn’t show the new max exp until they get more experience again, which is a little annoying. So if they had 49/50 and got 5 xp, the bar would update to say 4/50 instead of 4/100, but once they get say 2 xp it will update to 6/100 and be fine after that. I have looked this up on the Devforum and Youtube and I haven’t quite figured out what is causing the problem. Please see if you can see the issue.

-- Player-related variables
local player = game.Players.LocalPlayer
local level = player:WaitForChild("Level")
local current = level:WaitForChild("Current")
local max = level:WaitForChild("Max")

-- Ui-related variables
local gui = script.Parent
local exterior = gui:WaitForChild("Exterior")
local label = exterior:WaitForChild("Label")
local exp = exterior:WaitForChild("Exp")
local bar = exterior:WaitForChild("Bar")

-- Change stats upon join

label.Text = "Level "..level.Value
exp.Text = current.Value.."/"..max.Value.." Xp"
bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)

level.Changed:Connect(function(val)
	label.Text = "Level "..level.Value
	exp.Text = current.Value.."/"..max.Value.." Xp"
	bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)
	
	for i = 1,5 do 
		local fireworks = Instance.new("Part") 
		fireworks.Shape = 0 
		fireworks.formFactor = "Symmetric" 
		fireworks.Size = Vector3.new(1,1,1) 
		fireworks.BrickColor = BrickColor.Random() 
		fireworks.CFrame = player.Character.Head.CFrame + Vector3.new(0,2,0) 
		fireworks.Parent = game.Workspace 
		game:GetService("Debris"):AddItem(fireworks, 2) 
		fireworks.Velocity = Vector3.new(math.random(-30,30),math.random(-30,30),math.random(-30,30)) 
	end 
end)

current.Changed:Connect(function(val)
	exp.Text = current.Value.."/"..max.Value.." Xp"
	bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)
end)

The max variable is tied to the maxExperience value that is the problem. Thank you for taking the time to help me with my problem!

I had this problem too like a long time ago but managed to fix it

add this into the script

max.Changed:Connect(function() 
    exp.Text = current.Value.."/"..max.Value.." Xp"
	bar.Size = UDim2.new(current.Value/max.Value, 0, 1, 0)
    label.Text = "Level "..level.Value
end)
1 Like

I feel kind of dumb for not realizing that I should have just made a function for when the change occurs… smh. I’ve only been coding for like 3-4 days or so other than in 2020 when I watched a youtube tutorial lmao

Thank you for your help! It’s nice to have this annoying problem fixed!

No problem man. Glad to help :wink:

1 Like
max.Changed:Connect(function(newmax) 
	exp.Text = current.Value.."/"..newmax.." Xp"
	bar.Size = UDim2.new(current.Value/newmax, 0, 1, 0)
	label.Text = "Level "..level.Value
end)

Don’t forget the “New Value” parameter of .Changed events (only valid for instances which are suffixed by the word “Value”).

1 Like

Thank you my guy, but the problem was already solved.