Adding Remaining XP to XP Bar

So I had created a EXP system where if your exp was higher then the max exp needed it would carry over, and it works fine but the problem is the bar will not tween all the way to end instead it goes to the appropriate position for when all the xp has been given. If I give the player 1mil xp I want the xp bar to reach all the way to end and reset at the beginning multiple times until it reaches the correct level and bar amount. Not just go level 1 to 59 or whatever in an instant with some xp and bar in place. As you can see in the video I gain 50 exp each time but once i reach the limit it goes all the way to the beginning without reaching the end and starting over like how I would want it to be.

--This is the code for the EXP fyi the amount is decided from a parameter from exp module script so in this case im passing in the number 50 as the amount--
--(a module script)
local xpDifference = EXPNeeded.Value - Exp.Value

	local leftOverXP = Amount - xpDifference
	for i = 1,Amount do
		Exp.Value = Exp.Value + 1
		if Exp.Value >= EXPNeeded.Value then
			Exp.Value =  Exp.Value - EXPNeeded.Value
			Level.Value = Level.Value + 1
			EXPNeeded.Value = EXPNeeded.Value + 200


--This is the code for the EXP Bar (Local script 4 gui)
	EXPText.Text = "EXP:"..Exp.Value.."/"..EXPNeeded.Value
	local change = (Exp.Value / EXPNeeded.Value)
	local exp = math.clamp(change, 0, 1)

Could add an if statement to check whether the next exp addition will surpass the current exp cap…
Then you would just fill the bar to the max before running your Exp bar tween

1 Like

Maybe put the tween size to be the xp value size under the if Exp.Value >= EXPNeeded.Value then statement? Then after you play your tween, you will wait for how many seconds it takes before adding a level and making your exp value back to 0.

1 Like

This is because whenever you level, the EXP value never actually goes to 100, instead it just adds 1 to the levels value and changes the EXP to 0. This causes the tween to go straight to the result (0) rather than going to 100 then going back down to 0.
Sorry if I’m wrong.

I’ve solved this issue with my own experience bar script using the following formula
| (experienceNeeded - experienceLeft) / experienceNeeded |
This formula returns a percentage (or decimal) of the amount of experience is left. In this formula, if the returned value is 1 then that represents 100%. Following that logic, 1.5 is 150%, and .5 is 50%.

Then I simply loop through my level up calculation and update the GUI accordingly.

Concrete example

Helper Functions


--FORMULA: (experienceNeeded - experienceLeft) / experienceNeeded
-- This function will be used to update the experience bar to the new value
-- when the formula returns > 100%
function updatePlayerExperienceBar(player, playerInfo)
	onPointGain:FireClient(player, playerInfo.experiencePointsLeft, playerInfo.experiencePointsNeeded, playerInfo.level) -- Let the GUI know to update

--FORMULA: (experienceNeeded - experienceLeft) / experienceNeeded
-- This function will be used to max out the experience bar and reset it
-- when the formula returns <= 100% experience gain
function setPlayerExperienceBar(player, experienceLeft, experienceNeeded, level) 
	onPointGain:FireClient(player, experienceLeft, experienceNeeded, level) -- This can be refactored to be it's own event

-- This function simply levels the player up, and calculates the amount of experience needed for
-- the next level, and the amount of current experience
function updatePlayerInfo(playerInfo, points, canLevelUp) -- Simple function to do calculations and update the players information
	if canLevelUp then
		playerInfo.level += 1 -- Level Player Up
		playerInfo.experiencePoints = math.abs(playerInfo.experiencePointsLeft) -- Carry over experiences points
		playerInfo.experiencePoints = playerInfo.experiencePoints + points -- Add experience points
	-- Personal function used to calculate experience needed for the next level
	playerInfo.experiencePointsNeeded = calculateExperiencePoints(playerInfo.level) -- How much experience required for next level
	-- Personal function that returns the amount of experience left until the next level is reached
	playerInfo.experiencePointsLeft = calculateExperiencePointsLeft(playerInfo.experiencePoints, playerInfo.level) -- How much experience left until player gains a level
	return playerInfo

Main Progression Script


-- Custom table that stores player information for the game
playerInfo = updatePlayerInfo(playerInfo, points, false) -- Player gained points

-- If experience points left is lower than zero then the player has leveled up.
if playerInfo.experiencePointsLeft <= 0 then
    -- The player may have gained a lot of experience so a loop is required.
        -- Player leveled from point gain
		playerInfo = updatePlayerInfo(playerInfo, 0, true) 

        -- Here we employ the formula and get the absolute value
		local percentage = 
        ((playerInfo.experiencePointsNeeded - playerInfo.experiencePointsLeft)
        / playerInfo.experiencePointsNeeded)				
        -- The player gained an entire level, so animate the GUI to show
        -- the player their experience has reached 100% for each level
		if percentage >= 1 then
			setPlayerExperienceBar(player, 2, 1, playerInfo.level)
		wait(.5) -- Allow tween to occur

    -- When the player can no longer level up exit the loop
	until playerInfo.experiencePointsLeft > 0		

    -- 	Update the players GUI to show experience bar progress 
    -- until the next level
	updatePlayerExperienceBar(player, playerInfo)
    -- Player did not level, simply update the experience bar with 
    -- accumulated experience
	updatePlayerExperienceBar(player, playerInfo) 

You should be able to utilize the formula within your own script to update the experience bar progression. I hope this gave you an idea how to adapt this formula to your own GUI.

Thank you! That worked! I can’t believe it was something as simple as that but that was definitely the issue thanks again!