What's the best way to make a leveling system?

Hi. I created a weapon that increases its stats the more you use it. I made a level system that constantly checks if the player has enough EXP to increase the guns stats. It works fine and all, but I feel like im stressing out the server by constantly changing the ModuleScript for the gun stats even though its the same one. I wonder if theres an alternative method that’s not too stressful for the server to handle.

Currently, im using this method:

--Weapon Info ModuleScript, where the rank requirements are stored and other things

local Info = {
	NormalMesh = "rbxassetid://95387789",
	
	GoldenMesh = "rbxassetid://126534866",
	
	LevelEXP = {
		[2] = 500,
		[3] = 1250,
		[4] = 2000,
		[5] = 3000,
		[6] = 4000,
		[7] = 6500,
		[8] = 8000,
		[9] = 10000,
		[10] = 12500,
		[11] = 15000,
		[12] = 18000,
		[13] = 21250,
		[14] = 25000,
		[15] = 30000,
		[16] = 32847234,
		}
	}

return Info

The reason rank 16 is alot higher than the others is that i plan for Rank 15 to be the max rank.

Heres the code that runs everytime a player hits a target

p.GunEXP.Value = p.GunEXP.Value + exp
local wepinfo = require(game.ServerStorage.WeaponStats.Luger.Info) -- Info ModuleScript, seen above
local val = p.GunEXP.Value
local rank
	
for i,v in pairs(wepinfo.LevelEXP) do
	if val >= v and wepinfo.LevelEXP[i+1] > val then
		rank = i
		print("Ranked up! "..i)
		local newmodule = game.ServerStorage.WeaponStats.Luger["LV"..rank]:Clone()
		wep.Stats:Destroy()
		newmodule.Name = "Stats"
		newmodule.Parent = wep
	end
end

After ranking up once, The print command runs every single time the player hits a target, meaning it will also constantly replace a new modulescript for the gun. Im worried it might stress the server too much, especially if im dealing with a full auto weapon. Is there an alternative way to make a Leveling system(with a cap) without repeating unnecessary code?

4 Likes

It would be easier to make an algorithm for finding how much experience the next level requires.

An easier alternative is keeping a variable that holds the value of the next rank, then just check if the current amount of EXP is greater, if so, then rank up.

1 Like

@jonbyte is speaking straight facts. It’s pretty easy to do that.
If you want the same amount of experience for all levels tho, try this:

level = math.floor(xp / XP_REQUIRED_FOR_LEVELUP)

To get remaining XP, do:

remainder = xp % XP_REQUIRED_FOR_LEVELUP

1 Like

I replied to a similar post a while back. You could try something similar.

The key is application of a formula. As for a max rank, that’s where you use clamping for - not assigning the next rank to a massive value which will present issue should someone reach it. There’s been a couple threads on this, such as the one above, as well as various free models you can reference code from.

You can still go with a hardcoded amount of EXP required per level as per your dictionary method, but again, implement clamping if any certain maximums need to be met. I do know a production game that does this for its levelling. A formula would help out with arbitrary levelling, so you can avoid writing out a large amount of ranks. For example, Phantom Forces has a level up formula of (currentLevel+1)*1000.

1 Like

Wouldn’t that find the remainder of the exp left after you divided it. I would think that you would need to create an operation such as remainder = xp - XP_REQUIRED_FOR_LEVELUP. Of course I’m probably wrong because I’m new to scripting so please correct me if I am wrong.