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
----------------------------
-- 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
end
--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
end
-- 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
else
playerInfo.experiencePoints = playerInfo.experiencePoints + points -- Add experience points
end
-- 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
end
Main Progression Script
----------------------------
-- FUNCTION PROGRESSPLAYER
----------------------------
-- 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.
repeat
-- Player leveled from point gain
playerInfo = updatePlayerInfo(playerInfo, 0, true)
-- Here we employ the formula and get the absolute value
local percentage =
math.abs
((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)
end
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)
else
-- Player did not level, simply update the experience bar with
-- accumulated experience
updatePlayerExperienceBar(player, playerInfo)
end
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.