How do you make an XP/Level up system?

I’m working on an RPG game, where you can complete quests and get rewards such as money and XP, when you get enough XP you can level up.
The problem is I am trying to create something where if you have enough XP you can level up.
It’s a script inside of ServerScriptService, no errors, and I probably did some obvious error I wouldn’t know about because it’s my first time trying to do something like this.

local MaxLevel = 15
game.Players.PlayerAdded:Connect(function(player)
	while player.leaderstat.Level < MaxLevel do
		if player.leaderstat.XP > player.leaderstats.Level * 49 then
				player.leaderstats.Level = player.leaderstats.Level + 1
			end
	end
end)
1 Like

I’m assuming that you have placed some kind of intvalue or equivelant into leaderstats. If so you need to put .Value after XP and level.

3 Likes

Yeah, I forgot to put .Value. Even after putting those it wouldn’t work

local MaxLevel = 15
game.Players.PlayerAdded:Connect(function(player)
	while player.leaderstat.Level.Value < MaxLevel do
		wait()
		if player.leaderstat.XP.Value > player.leaderstats.Level.Value * 49 then
				player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
			end
	end
end)

Pretty sure you gotta name it ‘leaderstats’ not leaderstat, as well.

1 Like

yeah, I’m forgetting to add some stuff sorry

local MaxLevel = 15
game.Players.PlayerAdded:Connect(function(player)
	while player.leaderstats.Level.Value < MaxLevel do
		wait()
		if player.leaderstats.XP.Value > player.leaderstats.Level.Value * 49 then
				player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
			end
	end
end)

I’m wondering, does it have to do with this?

if player.leaderstats.XP.Value > player.leaderstats.Level.Value * 49 then

It depends. Does the XP ever become greater than the level*49? You could achieve this by editing the values manually via the server (not the client). Try also adding some prints to see why it isn’t working.

1 Like

I got rid of the maxlevel part, and just made it this and it works

game.Players.PlayerAdded:Connect(function(player)
	while wait() do
		if player.leaderstats.XP.Value > player.leaderstats.Level.Value * 49 then
				print('working')
				player.leaderstats.Level.Value = player.leaderstats.Level.Value + 1
			end
	end
end)

I use the following function whenever I add XP to the player:

function data:LevelUp()
	while self.Data.XP >= Exponentials.XPNeeded(self.Data.Level) do
		self.Data.XP = self.Data.XP - Exponentials.XPNeeded(self.Data.Level)
		self.Data.Level = self.Data.Level + 1
	end
	
	self:Update() -- update is a function to change the leaderstats
end

my usage is typically

	self.Data.XP = self.Data.XP + 100
	self:LevelUp()
4 Likes

That suggests that the ‘level’ was never less than ‘max level’. Although there could be issues with syntax, not sure.

2 Likes

Everyone else gave you that info, but make sure to use datastore! Roblox Developer DataStore Article

I use datastore for my leaderstats

1 Like