function Level:setXp(userId: number, xp)
local playerObject = Player.find(userId)
local now_xp = playerObject:getValue("Xp")
local now_level = playerObject:getValue("Level")
local now_maxXp = playerObject:getValue("Max_Xp")
local xp_multiplier = playerObject:getValue("Xp_Multiplier")
xp = (xp * xp_multiplier)
local finalXp = (xp + now_xp)
if now_level == MAX_LEVELS then return end
playerObject:setValue("Xp", finalXp)
if finalXp >= now_maxXp then
local reasing = (finalXp - now_maxXp)
playerObject:changeValue("Xp", 0)
playerObject:setValue("Level", now_level + 1)
playerObject:setValue("Competances_Points", 1)
playerObject:setValue("Max_Xp", 100)
if reasing > 0 then
self:setXp(userId, reasing)
end
end
end
What do you need help with? All you sent was your script.
Normally at level 7, the max level should be set to 700, but in my case, it’s setting it to 300.
Your code is very hard to debug, since you’re using things like
Player.find(userId)
which are not how you usually do things in Roblox. We can only really help you if you provide the full code in that case.
if reasing > 0 then
self:setXp(userId, reasing)
end
We can only tell you general things, such as: Don’t use recursion, it is evil.
I would suggest you write this function without it, that should solve your problem, since it’s occuring when you exit your recursion:
if now_level == MAX_LEVELS then return end
PS:
I don’t understand why you made wrappers around :SetAttribute() / :GetAttribute()
playerObject:getValue("Xp_Multiplier")
playerObject:setValue("Xp", finalXp)
So, using self:setXp(userId, reasing) is bad because it could lead to an infinite loop if reasing keeps being greater than the required XP (now_maxXp
). There are also some things I want to mention aswell, playerObject:changeValue(“Xp”, 0) after leveling up. If changeValue
is an incremental function, this might not do what you want, because it wont set the XP to zero. Maybe try using playerObject:setValue("Xp", 0)
if the intent is to completely reset it. I also don’t understand why you made wrappers around :SetAttribute() and :GetAttribute()
I made a better understanding and fixed up code for you though.
function Level:setXp(userId, xp)
local player = Player.find(userId)
if not player then
print("Player not found!")
return
end
-- This fetches the current values
local currentXp = player:getValue("Xp")
local currentLevel = player:getValue("Level")
local maxLevel = MAX_LEVELS
local maxXp = player:getValue("Max_Xp")
local xpMultiplier = player:getValue("Xp_Multiplier")
-- This applys the XP multiplier
local totalXpToAdd = xp * xpMultiplier
local newXp = currentXp + totalXpToAdd
-- This caps the level at max level
if currentLevel >= maxLevel then
print("Player has reached max level.")
return
end
-- This updates XP and handles leveling up
while newXp >= maxXp and currentLevel < maxLevel do
currentLevel = currentLevel + 1
newXp = newXp - maxXp
-- This updates the player stats
player:setValue("Level", currentLevel)
player:setValue("Competances_Points", player:getValue("Competances_Points") + 1)
print("Player leveled up! New level: " .. currentLevel)
-- This increase the max XP for next level (example growth rate of 20%)
maxXp = math.floor(maxXp * 1.2)
player:setValue("Max_Xp", maxXp)
end
-- This updates the remaining XP
player:setValue("Xp", newXp)
print("Player's current XP: " .. newXp)
end
I hope this helps you!