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.
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 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.
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
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