Question With Leveling System

Hello!

I’m making a leveling system, but I’m not sure how to do one thing. I want it so every 20 xp, you get a level up. How do I make it so there is inf levels? (So in the code you don’t have to write it and make a limit to levels.)

I probably should know this, or my brain just isn’t working.

Thanks,
Ben

Could you just set their level to their XP divided by 20? Or do you want it to be 20 more per level each time?

Usually no matter how Level is determined (except when it’s random), there is some sort of formula you can use to calculate it.

1 Like

Make xp and level both different values. Whenever you get 20 or higher you add 1+ to level.
until xp is not 20 or higher. (Divide xp by 20 until it can’t and every time it divides, add 1+ to level)

If you mean that whenever you level up for example:

for level 1 it takes 20 xp but for level 2 it takes 40 xp then.

Multiply 20 by the level for the requirement. The rest is said above.

If you repetitively divide, then this would mean levels would have their XP counterparts increasing exponentially. I really doubt he wants levels to be 20x harder to attain each time.

Level 1: 20 XP
Level 2: 400 XP
Level 3: 8,000 XP

It’s quite the increase.

1 Like

level 1 x 20 = 20xp
level 2 x 20 = 40xp
level 3 x 20 = 60xp

right? ???

That would be ideal, yes. However you suggested taking the XP (in my example we will use 50,000) and dividing it by 20 consecutively until you can no more.

The iteration cycle would go as follows:
50,000 / 20 = 2,500
2,500 / 20 = 125
125 / 20 = 6.25

They would only be level 3 with 50,000 XP.

I was thinking this, perferably.

You really don’t need to do what you did. Here are some tips which I’d recommend for making a level up system

  1. First add a folder named “LevelConfig”
  2. Second, add 3 number values named “Level”, “CurrentXP” and “XPLeft”
  3. Third, you can check if CurrentXP is more than XPLeft. If it is then you can subtract like this (CurrentXP - XPLeft). Then you can add a new level.
  4. Fourth, you can use DataStoreService to save the level and CurrentXP and XPLeft

I just wrote a quick module for XP to Level conversion, and vice versa. I also included a bonus function for calculating how much progress a player has made towards their next level (e.g. Player1 is 25% of the way to level 4).

																																														--[[
[Module Documentation]:
	
	Provides level calculation functions.
	
	Methods [Module]:
	
		[int]			Module:ToLevel({int} XP)			Converts the specified XP into a level.
		[int]			Module:ToXP({int} Level)			Converts the specified level into an XP count.
		[Dictionary]	Module:GetProgressInfo({int} XP)	Describes needed XP and level prpgress based on specified XP.
	
																																														--]]

-- // Module Configuration

local Settings = {
	XPIncrease = 20,
}

local Module = {}

-- // Public Functions

function Module:ToLevel(XP)
	local Result = 0
	local Last = 0
	
	while true do
		Last += Settings.XPIncrease
		XP -= Last
		
		if XP > 0 then
			Result += 1
		else
			break
		end
	end
	
	return Result
end

function Module:ToXP(Level)
	local Result = 0
	local Last = 0
	
	for i = 1, Level do
		Last += Settings.XPIncrease
		Result += Last
	end
	
	return Result
end

function Module:GetProgressInfo(XP)
	local CurrentLevel = self:ToLevel(XP)
	local CurrentLevelXP = self:ToXP(CurrentLevel)
	local NextLevelXP = self:ToXP(CurrentLevel + 1)
	local LevelProgress = XP - CurrentLevelXP
	
	return {
		NeededXP = NextLevelXP - CurrentLevelXP - LevelProgress,
		Progress = LevelProgress / (NextLevelXP - CurrentLevelXP),
	}
end

return Module

Let me know if you run into any issues with it. It’s adapted from the LevelModule in my own game.

local levelValue = Instance.new("IntValue") 
-- define your intvalue representing levels
local expValue = Instance.new("NumberValue")
-- define your numbervalue representing exp obtained
local maxExp = Instance.new("NumberValue")
-- define max exp needed before leveling up

local function LevelUp()
levelValue.Value += 1
expValue.Value = 0
maxExp.Value = 20
-- basic max exp algorithm:
-- maxExp.Value = (20 * levelValue.Value) ^ 1.618
end

local function GainExp(gain)
expValue.Value += gain
if expValue.Value > maxExp.Value then
    LevelUp()
end
end

while true do
GainExp(20)
wait(20)
end

Also if the code does nothing it means you literally ctrl+c ctrl+v my code

Parent the levelValue, expValue and maxExp to a leaderstats or something