Help with Battlepass Script

So, I have a battlepass script, it works kinda well, but there’s one issue. When I reach, level 2, for example it keeps the 100 XP that I needed for level 1. I want it to reset visually, but not entirely reset data for it. I wouldn’t want to change the value in my player entirely since, I store all the total XP on it.

local xp = game.Players.LocalPlayer.2024BattlepassXP.Value

local currentLvl = 0
local TotalXPNeeded = 100
local NextLvlXPNeeded = 100

print("Player is Level "..currentLvl..", has "..xp.." XP altogether.")

local function updateProgress()
	script.Parent.View.ProgressBar.Progress.Size = UDim2.new(xp / NextLvlXPNeeded, 0, 1, 0)
	script.Parent.View.ProgressBar.Level.Text = "Level "..(currentLvl + 1)..":"
	script.Parent.View.ProgressBar.XP.Text = xp.."/"..NextLvlXPNeeded.." XP"
end

updateProgress()

if currentLvl == 0 then
	--1
	TotalXPNeeded = 100
	NextLvlXPNeeded = 100
	if xp >= TotalXPNeeded then
		currentLvl += 1
		--2
		TotalXPNeeded = 250 
		NextLvlXPNeeded = 150
		updateProgress()
		if xp >= TotalXPNeeded then
			currentLvl += 1
			--3
			TotalXPNeeded = 400
			NextLvlXPNeeded = 150
			updateProgress()
			if xp >= TotalXPNeeded then
				currentLvl += 1
				--4
				TotalXPNeeded = 600
				NextLvlXPNeeded = 200
				updateProgress()
				if xp >= TotalXPNeeded then
					currentLvl += 1
					--5
					TotalXPNeeded = 1000
					NextLvlXPNeeded = 400
					updateProgress()
--then it goes on until level 20...

First of all- do not do this.
It would be soooo much better if you just had a table storing the values needed for each level and use that instead of billions of nested if/else statements.
(ex:)

local data = {
   [1] = { TotalXPNeeded = 250, NextLvlXPNeeded = 150 }, -- data for level 1
   [2] = { TotalXPNeeded = 400, NextLVlXPNeeded = 150 },
   -- ...and so on
}   

currentLvl += 1;
TotalXPNeeded = data[currentLvl].TotalXPNeeded;
NextLvlXPNeeded = data[currentLvl].NextLvlXPNeeded;    
updateProgress(); 

and for the visual stuff I’m assuming something like this is probably what you are looking for?
(if not let me know)

thing.Size = UDim2.new((xp - prevLvlTotalXPNeeded) /  (nextLvlTotalXPNeeded - prevLvlTotalXpNeeded), 0, 1, 0);   
thing2.Text = `Level { (currentLvl + 1) }:`;  -- use string literals (with backticks ``) to make inserting vars easier :)
thing3.Text = `{ xp - prevLvlTotalXPNeeded }/{ nextLvlTotalXPNeeded - prevLvlTotalXPNeeded } XP`;   

Next time try and have a seperate variable for total XP… it will make things a lot easier.
(btw, i didnt test any of this, so if theres errors [or you need help understanding something], please reply and ill be happy to help again)

Yeah, I kinda got lazy and just started copy and pasting stuff instead of making a clean looking script.

Anyway just with a little bit of editing I got it to work, so, nice.

1 Like