Level system calculation issue

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.

1 Like

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)