Let's say you have a game about pressing a button, each time you press it, the XP goes up, and if you collect enough, you level up. Something people normally do wrong, is setting a linear ecuation for the level system, wich will at some point make leveling up super hard to do. By dividing, it will make leveling up the same at some point. So nerds like me use another mathematical trick.
A logarithm is a quantity representing the power to which a fixed number (the base) must be raised to produce a given number. To understand it better, lets have the next example
local Logarithm = math.log(1000,10)
print(Logarithm)
Output: 3
why did the output print 3? The output said 3 beacouse 10(Input Y) ^ 3(Output) is 1000(Input X)
We use math.log(x,y) to calculate the logarithm of x in base y.
If you draw a line joining all the solutions with the Base 2, you'll have a line, wich joins all the powers of 2 in a circular way. Let's check some of the first powers of 2:
1 2 4 8 16 32 64 128 256
You can see that each time we hit a power of 2, the difference highers, but if we draw a line that pass through all of the numbers, you'll see that each time the line hits a power of 2, it increases less and less.
This will help us make a level system, so lets start it
local Level = 1
local XP = 0
local PointsForNextLvl
function AddXP(N)
PointsForNextLvl = math.ceil(math.log(Level+1,1.5)*100)
if XP+N >= PointsForNextLvl then
XP = math.abs(PointsForNextLvl-(N+XP))
Level = Level+1
else
XP = XP+N
end
end
Let's explain it. First, the Values: Level, is the level the player has, you need to change it to the actual player's level value, same for XP. PointsForNextLvl represents the number of XP you need for the next lvl.
The function below is to add Experience Points: Points for next lvl will change to the rounded logarithm of the players level with a base of 1.5 times 100. It will check if this value is lower than the XP the player is gonna get, and if it is, It'll level up the player and change the XP to the absolute value of the difference between points for next lvl and the XP the player is gonna get+the current XP (|a-b|) wich is a way to say the numbers between two numbers,even if one is negative. If the points for the next level is higher than the points the player will get, it will just change the XP.
The XP Value should be a Number Value
So it should work perfectly now, if you feel that the rate of leveling up is too slow or too fast, change the base of the logarithm (1.5) inside the line 6. Don't put 0 as it will result in a mathematical error.
Every game is centered about a game mechanic, it could be a tycoon, a simulator, a game where you have a super power, an RPG... Each time the player does the main mechanic, you should reward the player, in tycoons and simulators is simple, you do the game mechanic, you earn money, and you then buy things to make faster money. In an RPG, the player should be rewarded each time he/she completes a main mission. And in a game where you have a super power, you should make challenges with that game mechanic to make the players use it in order to get a reward, being each challenge, always original.
But if you make a way to earn XP other than using the game mechanic, or something important to the game. Then the reward should be worse than by doing the mechanic.
Lastly, if there's a broken way to earn XP. It should be by doing the game mechanic. As people wich break the game by a secondary mission or an optional challenge, will not experience the actuall game.
How is this connected to leveling up players? What if my game levels you up when you click a button?
The answer is simple:
If you haven't got the button yet:
If you already have the buton:
local Level = 1
local XP = 0
local PointsForNextLvl
local Power = 3 --Change this to set how op your button is--
function AddXP(N)
PointsForNextLvl = math.ceil(math.log(Level+1,1.5)*100)
if XP+N >= PointsForNextLvl then
XP = math.abs(PointsForNextLvl-(N+XP))
Level = Level+1
else
XP = XP+N
end
end
Script.Parent.MouseButton1Click:Connect(function()
AddXP(Power)
end)
The code shown before is a function. To use it, call the function:
AddXP(--Insert a number here--)
If you're using Datastore, or store the values in a different object, then delete the first 2 "local"s and then set them equal to the values you're using. This will make them global and allow them change
This DOES NOT WORK for checkpoints. This is a Level system, but a Level system of EXPERIENCE POINTS. If you want a checkpoints, open the toolbox and pick the one made by ROBLOX, wich is the official one.
I've seen lot's of comments of people hating this tutorial beacouse they don't know how to connect this. Look
First you code the way you get XP, then you use the print("Grant XP") function to test that it works, then you put the first code I gave you at the start of the script. Finally, you replace the function print("Grant XP") with the function AddXP(1) and replace 1 with whatever you want.
And also watch more tutorials, 'cause if I gotta explain this, you guys need to invest
Feel free to comment and make a feedback, I really like it. C ya in the next tutorial!