So I’m trying to create a leveling system with XP, but it gives me -50 XP, I really don’t know what I did wrong!
local function AddExp(amount, player)
local chosenPlayer = game.Players:WaitForChild(player)
local playerLevel = chosenPlayer:WaitForChild("leaderstats"):WaitForChild("Level").Value
local playerExp = chosenPlayer:WaitForChild("InvisibleStats"):WaitForChild("XP").Value
local ExpForLevelUp = (50+50*playerLevel)
playerExp = playerExp + amount
local playerExpAfter = chosenPlayer:WaitForChild("InvisibleStats"):WaitForChild("XP").Value
if ExpForLevelUp >= playerExpAfter then
playerExpAfter = playerExpAfter - ExpForLevelUp
playerLevel = playerLevel + 1
elseif ExpForLevelUp <= playerExpAfter then end
end
Your system is conceptually flawed. Make sure you force the player to earn more XP as they grow on levels. You are doing that, but you are doing it recursively. Make two functions:
A function that converts level to needed XP.
A function that converts XP to what level the player is at.
You need a better abstraction of what you are doing. You are in the correct direction though!
Once you have those functions in place, notice that they are inverse functions, meaning f1^-1 must be f2^1 and vice versa. This lets you get a lot of precision and avoid iteration (since you are using mathematical operations).
Now that you have that in place, you can just do stuff like find out the XP you need for the next level. You can just subtract the net XP for the next level and the net XP for the current level. You can divide that by the current XP - current level to net XP to get the progress.
Edit: I used a similar system in Bash and it works perfectly. It is up to you to figure out the functions before you put it into the code.
Do this, Because i belive that the ExpForLevelUp will reset whenever you call this function in the the viersion that you made.
local ExpForLevelUp = 50 --XP you need for level up
local XpUp = 50 --how much more you need next time
function AddExp(amount, player)
local chosenPlayer = game.Players:WaitForChild(player)
local playerLevel = chosenPlayer:WaitForChild("leaderstats"):WaitForChild("Level").Value
local playerExp = chosenPlayer:WaitForChild("InvisibleStats"):WaitForChild("XP").Value
playerExp = playerExp + amount
if playerExp >= ExpForLevelUp then
ExpForLevelUp = ExpForLevelUp + XpUp
playerLevel = playerLevel + 1
end
end
Also i found whats bothering your script its this line
playerExpAfter = playerExpAfter - ExpForLevelUp --Look at the minus
Its supposed to be
playerExpAfter = playerExpAfter + ExpForLevelUp --Look at the plus
So your hole script shoud be this, unless you do the one i put at the beginning
local function AddExp(amount, player)
local chosenPlayer = game.Players:WaitForChild(player)
local playerLevel = chosenPlayer:WaitForChild("leaderstats"):WaitForChild("Level").Value
local playerExp = chosenPlayer:WaitForChild("InvisibleStats"):WaitForChild("XP").Value
local ExpForLevelUp = (50+50*playerLevel)
playerExp = playerExp + amount
local playerExpAfter = chosenPlayer:WaitForChild("InvisibleStats"):WaitForChild("XP").Value
if ExpForLevelUp >= playerExpAfter then
playerExpAfter = playerExpAfter + ExpForLevelUp
playerLevel = playerLevel + 1
elseif ExpForLevelUp <= playerExpAfter then end
end