XP Level Up isn't working correct

I am still in the stages of learning and I am working on a leaderstats board with an XP leveling system. Right now everything is working minus the leveling system. The leveling system works up to when you get enough XP to reach level 1, after that the player level goes up with every kill. It doesn’t go up based off XP earned.

My goal was to figure out how to add 100 required XP every time the players levels up. Basically have 150 XP required for level 1 and then level 2 would be 250, level 3 be 350 etc. The XP should reset to zero at every level up. I have attached the code I am working with below.

leaderstats code

local function addBoard(player)
	local board = Instance.new("Folder", player)
	board.Name = "leaderstats"
	
	local level = Instance.new("IntValue", board)
	level.Name = "Level"
	level.Value = 0

	local kills = Instance.new("IntValue", board)
	kills.Name = "Kills" 

	local xp = Instance.new("IntValue", board)
	xp.Name = "XP"
	xp.Value = 0


end

local function onCharAdded(char)
	local hum = char:WaitForChild("Humanoid")
	hum.Died:Connect(function() 
		local tag = hum:FindFirstChild("creator")
		if tag then
			local ePlayer = tag.Value
			local kills = ePlayer.leaderstats.Kills
			kills.Value = kills.Value + 1
			local xp = ePlayer.leaderstats.XP
			xp.Value = xp.Value + 10
		end
	end)
end

local function onPlayerAdded(player)
	addBoard(player)
	player.CharacterAdded:Connect(onCharAdded)
end

game.Players.PlayerAdded:Connect(onPlayerAdded)

StarterPack has this localscript in it

local plr = game.Players.LocalPlayer 
plr.leaderstats.XP.Changed:connect(function()
	if plr.leaderstats.XP.Value > 149 then -- If they have more than 49 xp (50 +)
		plr.leaderstats.Level.Value = plr.leaderstats.Level.Value +1 -- Add one to their levels
		plr.leaderstats.XP.Value = 0 -- Reset player's xp to 0
	end
end)

First of all, handle the .Changed event on the same server script. Otherwise, XP will not show for other players and your game will break.

Also, you want to change 149 to the level up xp required - 1. So here’s how you do that:

if plr.leaderstats.XP.Value > (149 + (plr.leaderstats.Level.Value * 100)) then
1 Like

this?

if plr.leaderstats.XP.Value > 100 * plr.leaderstats.Level.Value + 50 then
1 Like

First thank you for replying and helping.

So do I move the .Changed event into the ServerStorage location? Is that what you mean? Or put it into the leaderstats code?

Your code is working when using it in the localscript in the StarterPack folder. I only notcied one thing with it though. When the player levels up the XP resets to zero and on the first kill the XP goes right back where it was. Basically player has 50 XP and levels up and now has 0 XP. The first kill throws them back to 60 XP.

You could make a variable for the XP Required to level up and then increase the XP Required by 100 every time they reach the goal of the XP Required, so do something like this:

local xpRequirement = 100

while true do -- checks every 1 second
     wait(1)
     if plr.leaderstats.XP.Value >= xpRequirement then
            plr.leaderstats.Level.Value += 1
            xpRequirement += 100
            plr.leaderstats.XP.Value = 0
     end
end

Don’t do that. Instead check when the XP changes.

Put it into the leaderstats code.

I know that code has to be input in a certain order so that it runs correctly, I believe and correct me if I am wrong on that. So where would I insert what you shared above in the leaderstats? I tried it earlier and it didn’t run .

I keep getting attempt tp index nil with that in the leaderstats code.

it is the plr part that is throwing that error in the output window. Not sure why but I am trying to figure it out.

1 Like

Add the code right in the game.Players.PlayerAdded event.

local plr = player
plr.leaderstats.XP.Changed:connect(function()
	if plr.leaderstats.XP.Value > 149 then -- If they have more than 49 xp (50 +)
		plr.leaderstats.Level.Value = plr.leaderstats.Level.Value +1 -- Add one to their levels
		plr.leaderstats.XP.Value = 0 -- Reset player's xp to 0
	end
end)
1 Like

Thank you! Really appreciate the your help and from everyone else as well. Learned more tonight and onto the next items.

1 Like